Windows 一旦文件位于目录中,就会自动复制和重命名该文件

Windows 一旦文件位于目录中,就会自动复制和重命名该文件,windows,powershell,batch-file,Windows,Powershell,Batch File,我对编程比较陌生,所以我需要一些帮助。 我有两个目录 C:\test1 C:\test2 因此,在测试中,1将不断获取文件。 看起来像这样: testA000_00001.txt0..txt test00A0_00102.txt1..txt test00A0\u 00102\u 00123.txt45..txt testG000_00999.txt999..txt testH000_00013.txt0..txt 因为test1中的文件必须保持它们在test2中需要的方式。 而且由于t

我对编程比较陌生,所以我需要一些帮助。
我有两个目录

  • C:\test1
  • C:\test2
因此,在测试中,1将不断获取文件。
看起来像这样:

  • testA000_00001.txt0..txt
  • test00A0_00102.txt1..txt
  • test00A0\u 00102\u 00123.txt45..txt
  • testG000_00999.txt999..txt
  • testH000_00013.txt0..txt
因为test1中的文件必须保持它们在test2中需要的方式。
而且由于test2需要是当前版本,所以需要在文件位于test1中的那一刻就执行该操作。
但是没有.txt0。-。txt999。部分。

  • testA000_00001.txt
  • test00A0_00102.txt
  • test00A0_00102_00123.txt
  • testG000_00999.txt
  • testH000_00013.txt

同样重要的是,这些文件只被复制一次,因为它们不会在test2中停留很长时间。

我用xcopy和其他一些版本的copy进行了尝试,但每次它都会将文件复制回test2
,在我将文件从test2移动后,文件会再次复制到test2中。

(sry还不能评论)

我没有确切的代码(正在处理) 我认为可行的方法是:

  • 检查一个文本文件是否有内容
  • 安排每分钟左右运行一次任务
  • 移动到所需的文件夹
  • 使用dir命令获取当前目录中的所有文件
  • 抄袭
  • 将这些内容写入文本文件
  • 改名
  • 环路
当我完成代码时,我将进行编辑。 同时,在它们进入目录时将它们按顺序移动是非常耗费资源的,但如果代码非常高效,则可能会这样做

#make a folder
md "C:\test\test3"
#move to test1
cd "C:\test\test1"

# Presets
$txt = ".txt"
$files = dir
#clean up the files list
$files = $files -split "`r`n"

#for each file it found 
foreach ($line in $files) {
#move the file from test1 to test 3
    Copy-Item "C:\test\test1\$line" -destination "C:\Test\test3"
    #take the first 14 chars from the file name
    $line_name = $line.Substring(0, 14)
    #add a .txt extension
    $line_name = $line_name + $txt
    #rename the files in test3
    Rename-Item -path "C:\test\test3\$line" -NewName "$line_name"
    #move the files to test 2 and if they alr exist replace(update) them
    Move-Item -Path "C:\test\test3\$line_name" -Destination "C:\Test\test2" -Force
    }
    Remove-Item –path "C:\test\test3" –recurse -force
这段代码非常接近工作状态,但我今天可能无法完成。 它仅无法移动文件:test00A0_00102_00123.txt45..txt
由于名称不起作用。当脚本完全运行时,我将更新它。

@noraga请将其标记为anwser(投票下面的小复选标记)