简体   繁体   中英

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). My script is as follows:

  • copy /y myfile.txt %backuppath%

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%.

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.

@echo off
set "backuppath=c:\folder"
for /r %%a in (*) do (
    if exist "%cd%\%%~nxa" (
       echo copying "%%a"
       copy "%%a" "%backuppath%" >nul
    )
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM