0

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?

0

2 Answers 2

1

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
    )
)
Sign up to request clarification or add additional context in comments.

1 Comment

A better method that occurs to me now is to check that the drive:\path of the file is equal to the current directory. The code above can copy files twice or more if the same filename exists in the current directory as well as somewhere else in the directory tree.
1

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.

2 Comments

I wish I could just do that. I have to create a batch script for my class that does exactly what the question says :-(
you should be able to but you have to include a path also look at Computer hope.com for more commands and a better explanation of them

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.