0

I want to create simply batch script to copy my folders from my user profile to NAS using robocopy. I have written the script but it is not working. I have two files one is the script and second is the text file where I will put the folder paths to be copied to NAS. The scipt will use this text file. Following are the code snippets, batch file:

@echo off

for /f "tokens=*" %%v in (dir.txt) do (
echo:%%v
robocopy %%v h:\ 
)

text file (dir.txt)

"%userprofile%\desktop"
"%userprofile%\downloads"
"%userprofile%\My Documents"
"%userprofile%\Documents"
"%userprofile%\Favorites"

the batch script is unable to process file names from variable.

Thanks

1
  • Are the folders that need to be copied always subdirectories of %userprofile%? Commented Jan 31, 2013 at 17:21

2 Answers 2

1

Forgive me if this answer is naive, but can you alter the source file? If it was simply this:

\desktop
\downloads
\My Documents
\Documents
\Favorites

You could use this batch file:

@echo off

for /f "tokens=*" %%v in (dir.txt) do (
echo:"%userprofile%%%v"
robocopy "%userprofile%%%v" h:\ 
)

If there is some reason that the userprofile of the one executing the command does not work, maybe make a batch program that generates the initial input file with the correct paths already spelled out.

Sign up to request clarification or add additional context in comments.

2 Comments

HI Gray,thats cool. Its working for me. Do you know why it does not take the userprofile in the script.
Glad it worked for you. I think it is because you are passing basically a literal string to the command rather than something it needs to interpret. Why does it work that way? I do not know. There may be some way to make it work with what you did by basically using the call command and piping the output? Not sure if that would work, but this is a much simpler solution (IMO).
1

As CharlesB suggested, I would write a script like this in PowerShell. It is much easier to use than batch scripts.

I think this script will do what you want:

cat dirs.txt | ForEach {
    $dirPath = $_ -replace  "%userprofile%", $env:userprofile
    Write-Host $dirPath
    robocopy $dirPath h:\
}

3 Comments

hi, thanks for the suggestions. We have lots of Windows XP installed and we want to keep a consistent set of script along the organization.
It is possible to install PowerShell on XP but you probably don't want to do that...
yes quite right just want to leverage the current tools i.e batch script.

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.