thetipsyhacker
thetipsyhacker

Reputation: 1422

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:

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?

Upvotes: 0

Views: 967

Answers (2)

CMS_95
CMS_95

Reputation: 335

try these commands in what ever order

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

Upvotes: 1

foxidrive
foxidrive

Reputation: 41224

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
    )
)

Upvotes: 1

Related Questions