CMD脚本:使用 “for” 循环

huangapple go评论48阅读模式
英文:

CMD script: using "for" loop

问题

你好,以下是你的翻译内容:

我需要编写一个简单的CMD脚本,它需要创建一个主文件夹和十个子文件夹。然后,我需要将以字母"a"开头的所有文件从system32复制到第一个子文件夹,将以字母"b"开头的所有文件从system32复制到第二个子文件夹,将其余的文件都复制到最后一个文件夹。

我只有在最后一部分遇到问题,我不知道如何排除已复制的文件。

希望这对你有所帮助!如果你需要进一步的解释或帮助,请告诉我。

英文:

I have to write simple CMD script, it has to create main folder and ten subfolders inside.Then I have to copy all the files that starts with "a" letter from system32 to the first subfolder, the files that starts with "b" letter from system32 to the second subfolder and all remaining to the last folder.

I have only problems with the last part, I don't know how to exclude previously copied files.

@echo off
rd /s /q %main%
echo Name of main folder
set /P main=
echo Names of subfolders
set /P subfolder=
mkdir %main%

for /L %%D in (1,1,10) do (mkdir "c:\Users\tomek\Desktop\%main%\%subfolder%_%%D")
for  %%Z in ("C:\Windows\System32\a*.dll") do (
copy %%Z "%main%\%subfolder%_1"
)
for  %%X in ("C:\Windows\System32\b*.dll") do (
copy %%X "%main%\%subfolder%_2"
)
for  %%Y in ("C:\Windows\System32\*.dll") do (
if not "!%%~nY:~0,1!"=="a" (copy %%Y "%main%\%subfolder%_10")
)

I've tried to exclude firstly files that starts with "a". The CMD syntax is really unintuitive to me, can someone help me?

答案1

得分: 1

for没有内部方法可以忽略某些内容,所以你必须自己处理 - 例如使用 findstr /i /v /b(查看 for /? 以了解这些开关的作用)(未记录的:使用 findstr,你可以将开关连接为 /ivb

for /f "delims=" %%Y in ('dir /b /a-d C:\Windows\System32\*.dll ^|findstr /ivb "a b"') do ( 

(由于你定义了10个子文件夹,我假设你想将A到I复制到前九个,所以“其余的”可以写为 dir ... |findstr /ivb "[a-i]"

英文:

for has no internal method to ignore somthing, so you have to do it yourself - for example with findstr /i /v /b (see for /? for what those switches do) (undocumented: with findstr, you can concatenate the switches as /ivb)

for /f "delims=" %%Y in ('dir /b /a-d C:\Windows\System32\*.dll ^|findstr /ivb "a b"') do (

(as you define 10 subfolder, I assume you want to copy A* to I* to the first nine, so "the rest" can be written as dir ... |findstr /ivb "[a-i]" )

huangapple
  • 本文由 发表于 2023年5月18日 01:48:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274892.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定