khan
khan

Reputation: 3

Windows batch script for processing filenames

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

Upvotes: 0

Views: 162

Answers (2)

Gray
Gray

Reputation: 7130

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.

Upvotes: 1

Stanley De Boer
Stanley De Boer

Reputation: 5241

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:\
}

Upvotes: 1

Related Questions