0

I'm trying to make a batch file to copy files from a text list to a folder. The code was copied in this forum but it doesn't work.

The text list is in the desktop.

list.txt

and contains, for example,

C:\Users...\Desktop\Test\item1.ipt
C:\Users...\Desktop\Test\item2.ipt
C:\Users...\Desktop\Test\item3.ipt
C:\Users...\Desktop\Test\item4.ipt
C:\Users...\Desktop\Test\item5.ipt
C:\Users...\Desktop\Test\item6.ipt

and my batch file is also in the desktop.

@echo off 
FOR /F "delims=" %%a IN (C:\Users\...\Desktop\list.txt) DO COPY "%%~a" "C:\Users\...\Desktop\Temp\%%~nxa"

Both files (batch and txt) are in the desktop, so can I delete the path of the text file?

@echo off 
FOR /F "delims=" %%a IN (list.txt) DO COPY "%%~a" "C:\Users\...\Desktop\Temp\%%~nxa"

Thanks for your help.

EDIT: The ideia is to get the file location in the text file, so directories in the text file are variables.

3
  • Possible duplicate of Batch: Copy a list (txt) of files Commented May 7, 2017 at 8:50
  • I saw that, Trimax, but it's not the same. I don't want to set a source dir. I want to copy all the line (path with the file) to a new folder, so the source dir is variable... Commented May 7, 2017 at 9:17
  • To search a file in the directory holding the batch file, use %~dp0list.txt; just using list.txt meaning .\list.txt points to the current working directory, which is not necessarily the same... Commented May 7, 2017 at 11:01

1 Answer 1

2
@echo off
pushd "%userprofile%\Desktop"
FOR /F "delims=" %%a IN (list.txt) DO COPY "%%~a" ".\Temp\%%~nxa"
popd

In the above code snippet:

Edit. Above ´for /F´ loop does do nothing silently if the list.txt is empty, or if it contains only empty lines and standard delimiters, or if it start with some control characters, e.g. NULL character, or if it is saved using UTF-16 encoding.:

==> type null_and_data.txt
 1st line
2nd
3rd

==> FOR /F "delims=" %a IN (null_and_data.txt) DO @echo "%~a"

==> powershell -c "(type null_and_data.txt -Encoding byte -TotalCount 16) -Join ','"
0,49,115,116,32,108,105,110,101,32,13,10,50,110,100,13

==>

Some of above scenarios (in particular, the latter one) could be solved by using type command as follows:

@echo off
pushd "%userprofile%\Desktop"
FOR /F "delims=" %%a IN ('type list.txt') DO COPY "%%~a" ".\Temp\%%~nxa"
popd

Example:

==> FOR /F "delims=" %a IN (fileUTF16LEBOM.txt) DO @echo "%~a"

==> type fileUTF16LEBOM.txt
x
y

==> FOR /F "delims=" %a IN ('type fileUTF16LEBOM.txt') DO @echo "%~a"
"x"
"y"

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

1 Comment

Thanks, JosefZ, but it doesn't work. No error messages, but nothing.

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.