[英]How do I copy files from the current directory, but not sub-directories using a batch script?
I have a school assignment and I am stuck on this one question. 我有一份学校作业,我坚持这个问题。 I have no idea where else to turn.
我不知道还能在哪里转。
So this question follows on from the previous question which is to make a script to copy "myfile.txt" to my environment variable %BackUpPath% (which is set to C:\\backup). 所以这个问题是从上一个问题开始的,即将脚本“myfile.txt”复制到我的环境变量%BackUpPath%(设置为C:\\ backup)。 My script is as follows:
我的脚本如下:
The question I'm stuck on asks me to make a script using an IF EXIST statement in conjunction with a FOR loop to copy all the files in the current directory, but not any sub-directories to %backuppath%. 我坚持的问题要求我使用IF EXIST语句结合FOR循环创建一个脚本,以复制当前目录中的所有文件,但不复制任何子目录到%backuppath%。
How should I write this script? 我该怎么写这个脚本?
按顺序尝试这些命令
@echo off tree "C:\\backup" find /c "*.TXT" C:\\Backup if exists "file path\\name" move /y "files within the folder to other folder" del /f /s "main file-path" for each %.txt IN C:\\Backup goto a <replace move with copy if needed> ;;this is a rough idea of what you might need.
This should solve the question, but it's academic rather than a real life code. 这应该可以解决问题,但它是学术性的,而不是现实生活中的代码。
It uses the recursive switch of for-in-do
and checks if the filename.ext
that is generated by the for-in-do actually exists in the current directory - and then copies those files. 它使用
for-in-do
的递归开关并检查由for-in-do生成的filename.ext
是否实际存在于当前目录中 - 然后复制这些文件。
@echo off
set "backuppath=c:\folder"
for /r %%a in (*) do (
if exist "%cd%\%%~nxa" (
echo copying "%%a"
copy "%%a" "%backuppath%" >nul
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.