Create Directory and Copy files from different directory (shared location) in production using Windows batch scripting

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

Create Directory and Copy files from different directory (shared location) in production using Windows batch scripting

问题

我有一个生产映射,其中文件夹中有更多文件需要从一个共享位置复制到另一个位置。

位置 A:D:\UK\UK0623\Path\Temp_Variable\ORIGINAL\test1.pdf,test2.pdf,.....

位置 B:D:\UK\UK0723\Path\Temp_Variable\Reissue\

如您所见,需要解决上述挑战,需要进行以下 3 个步骤:

  1. 从用户处获取父文件夹路径和子文件夹路径,并将它们分别保存在变量中,

父路径:D:\UK\UK0623\Path

子路径:D:\UK\UK0723\Path

  1. 从父路径中选择并保存第一个名称/文件夹名称作为 TEMP 变量,例如:D:\UK\UK0623\Path\Temp_Variable

  2. 循环遍历 TEMP 变量,因为我们有数百个文件夹名称可用,如 SNO_01、SNO_02 等。

  3. 进入原始文件夹(ParentPath + temp variable + \Original Folder)并复制原始文件夹中的文件,例如:D:\UK\UK0623\Path\Temp_Variable\Original...

  4. 对于子文件夹路径,将 TEMP 变量用于路径,然后创建一个 Reissue 文件夹,然后将文件粘贴到其中(ChildPath + temp variable + \Reissue Folder),例如:D:\UK\UK0723\Path\Temp_Variable\Reissue...

请注意:在生产环境中有数百个序列号或文件夹。

Reissue 文件夹需要为每个序列号 SNO_01、SNO_02... 在运行时创建。

我尝试了以下脚本,

  1. @echo off
  2. setlocal enabledelayedexpansion
  3. pushd D:\Uk
  4. SET CURRENT_DIR=%cd%
  5. SET PARENT_DIR=%CURRENT_DIR%\UKRR_0623
  6. SET CHILD_DIR=%CURRENT_DIR%\UKRR_0723
  7. SET TEMP_VAR=SNO_01
  8. echo %CURRENT_DIR%
  9. echo %PARENT_DIR%
  10. echo %CHILD_DIR%
  11. for %%i in ("%CURRENT_DIR%\UKRR_0623\%TEMP_VAR%\Original*.*") do (
  12. copy "%%i" "%CURRENT_DIR%\UKRR_0723\%TEMP_VAR%\Reissue")
  13. echo backup completed
  14. pause

它显示目录路径语法错误

编辑:

  1. @echo off
  2. setlocal enabledelayedexpansion
  3. pushd D:\Uk
  4. SET CURRENT_DIR=%cd%
  5. SET PARENT_DIR=%CURRENT_DIR%\UKRR_0623
  6. SET CHILD_DIR=%CURRENT_DIR%\UKRR_0723
  7. SET TEMP_VARS=SNO_01 SNO_02
  8. for %%i in (%TEMP_VARS%) do (
  9. xcopy /s "%PARENT_DIR%\%%i\Original\*.*" "%CHILD_DIR%\%%i\Reissue"
  10. )
  11. pause

D:\Uk>(xcopy /s "D:\Uk\UKRR_0623\SNO_01 SNO_02\Original*.*" "D:\Uk\UKRR_0723\SNO_01 SNO_02\Reissue" )File not found - .
0 File(s) copied

  1. 请注意,您在脚本中应该使用`%%i`而不是`%TEMP_VARS%`,以便正确循环遍历每个临时变量。
  2. <details>
  3. <summary>英文:</summary>
  4. I have a production mapping where the folder does have more files to copy from one shared location to the other location.
  5. Location A: D:\UK\UK0623\Path\Temp_Variable\ORIGINAL\test1.pdf,test
  6. 2.pdf,.....
  7. Location B: D:\UK\UK0723\Path\Temp_Variable\Reissue\
  8. As you see the above challenge, need to work on 3 steps
  9. 1. Get parent folder path and child folder path from the User and save it in separate Variable till
  10. parent path: D:\UK\UK0623\Path
  11. child path: D:\UK\UK0723\Path
  12. 2. from the Parent path, select and save the first name/folder name as a TEMP variable
  13. for example: D:\UK\UK0623\Path\Temp_Variable
  14. 3. Loop thru the TEMP_variable, because we have 100&#39;s of folder name available as SNO_01,SNO_02,etc..
  15. 4. get into the Original folder (ParentPath + temp variable + \Original Folder) and copy the files inside Original folder
  16. for example: D:\UK\UK0623\Path\Temp_Variable\Original\...
  17. 5. to the child folder path, use TEMP variable into the path then, Create a Reissue folder, then paste the files in it (ChildPath + temp variable + \Reissue Folder)
  18. for example: D:\UK\UK0723\Path\Temp_Variable\Reissue\...
  19. Please note: there are 100&#39;s of Serial numbers or folders are there in prod.
  20. Reissue folder needs to create runtime for every serial number SNO_01, SNO_02....
  21. I tried the below script,
  22. @echo off
  23. setlocal enabledelayedexpansion
  24. pushd D:\Uk
  25. SET CURRENT_DIR=%cd%
  26. SET PARENT_DIR=%CURRENT_DIR%\UKRR_0623
  27. SET CHILD_DIR=%CURRENT_DIR%\UKRR_0723
  28. SET TEMP_VAR=SNO_01
  29. echo %CURRENT_DIR%
  30. echo %PARENT_DIR%
  31. echo %CHILD_DIR%
  32. for %%i in (&quot;%CURRENT_DIR%\UKRR_0623\%TEMP_VAR%\Original*.*&quot;) do (
  33. copy &quot;%%i&quot; &quot;%CURRENT_DIR%\UKRR_0723\%TEMP_VAR%\Reissue&quot;)
  34. echo backup completed
  35. pause
  36. it shows the directory path syntax error
  37. EDIT:
  38. @echo off
  39. setlocal enabledelayedexpansion
  40. pushd D:\Uk
  41. SET CURRENT_DIR=%cd%
  42. SET PARENT_DIR=%CURRENT_DIR%\UKRR_0623
  43. SET CHILD_DIR=%CURRENT_DIR%\UKRR_0723
  44. SET TEMP_VARS=SNO_01 SNO_02
  45. for %%i in (%TEMP_VARS%) do (
  46. xcopy /s &quot;%PARENT_DIR%\%TEMP_VARS%\Original\*.*&quot;
  47. &quot;%CHILD_DIR%\%TEMP_VARS%\Reissue&quot;
  48. )
  49. pause
  50. D:\Uk&gt;(xcopy /s &quot;D:\Uk\UKRR_0623\SNO_01 SNO_02\Original\*.*&quot; &quot;D:\Uk\UKRR_0723\SNO_01 SNO_02\Reissue&quot; )File not found - *.*
  51. 0 File(s) copied
  52. </details>
  53. # 答案1
  54. **得分**: 1
  55. 首先,你的脚本无法正常运行:设置语法错误,变量使用错误,还有其他一些小错误...
  56. 1 - 为什么你以 `` \` `` 开始?请替换为只有 `` @echo off ``(同样适用于最后一行,只使用 ``pause``
  57. 2 - 你的设置也不正确,你需要使用以下语法(代码、等号和值之间没有空格+不需要在 ``%`` 中写所有内容,它是为变量准备的):
  58. ```shell
  59. set CODE=VALUE

3 - 要使用一个使用 set 定义的变量,你只需要在代码之间使用 %。要打印变量的内容,你可以这样做:

  1. echo %code%

当你运行脚本时,你会看到以下输出:

  1. VALUE

4 - 另一个小错误,当你使用路径时,请将其放在双引号中(以防止空格引起的错误)。所以要切换到你的 CURRENT_DIR 目录:

  1. cd &quot;%CURRENT_DIR%&quot;

5 - 你的 for 循环看起来不错,但确保所有的拷贝都在同一行上,并使用括号确保它将计算所有东西:

  1. for %%i in (&quot;%PARENT_DIR%\i\Original\*.txt&quot;) do (
  2. copy &quot;%%i&quot; &quot;%CHILD_DIR%\i\Reissue&quot;
  3. )

如果按照我说的修改一切,应该可以正常工作(不要忘记编辑你的问题以显示结果)。

如果需要进一步解释,请随时提问!

附言:如果这解决了你的问题,请不要忘记将其标记为答案!

英文:

First of all, your script CAN'T work : wrong set syntax, wrong usage of variables, other small mistakes...

1 - Why did you start with \` ? replace it with only @echo off (same thing goes for the last line, only use pause)

2 - Your sets are wrong too, you need to write them with the syntax (no spaces between the code, the equal and the value + no need to write everything between % its for the variables) :

  1. set CODE=VALUE

3 - To use a variable (defined with set) you just need to write the code between %. To print the content of a variable you can do :

  1. echo %code%

And you'll see the output below when you run the script

  1. VALUE

4 - Another small mistake, when you use a path, write it between double quotes (to prevent errors with white spaces). So to change directory to your CURRENT_DIR :

  1. cd &quot;%CURRENT_DIR%&quot;

5 - Your for loop looks good but be sure to have all your copy on the same line and use parenthesis to be sure it will count everyting :

  1. for %%i in (&quot;%PARENT_DIR%\i\Original\*.txt&quot;) do (
  2. copy &quot;%%i&quot; &quot;%CHILD_DIR%\i\Reissue&quot;
  3. )

If you change everything as I said it should work (don't forget to edit your question to show us the result).

Feel free to ask for further explanations !

PS : don't forget to mark this as the answer if it solved your problem Create Directory and Copy files from different directory (shared location) in production using Windows batch scripting

EDIT :

In answer at your comment :

>the temporary variable or Directory should change is SNO_01 from both source and destination path, I guess need to use for loop based on the SNO_01,SNO_02,SNO_03, etc...then I need to xcopy by changing the path, Can you help on how to loop this in both source and destination path

You need to loop through different names/values(and copy recursively the files in these folders), to do it, just place the xcopy command in the loop and use the loop variable %%i. In order to do it just do this :

  1. for %%i in (SNO_01 SNO_02 SNO_03 SNO_0N) do (
  2. xcopy &quot;%CURRENT_DIR%\%%i\Original&quot; &quot;%CURRENT_DIR%\%%i\Reissue&quot;
  3. )

The values between the parenthesis needs to be separated by spaces only and you can replace it by a variable to have a better format:

  1. SET TEMP_VARS=SNO_01 SNO_02 SNO_03 SNO_0N
  2. for %%i in (%TEMP_VARS%) do (
  3. xcopy &quot;%CURRENT_DIR%\%%i\Original&quot; &quot;%CURRENT_DIR%\%%i\Reissue&quot;
  4. )

huangapple
  • 本文由 发表于 2023年7月6日 21:10:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629207.html
匿名

发表评论

匿名网友

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

确定