You are not logged in.
Hi,
I need to write script in a .bat file for the following.
I need to get the filename of the files in a directory, then look at the second character of the file name, if second character of the file name is "1", then that file need to be moved into "Folder1", if the second character of the file name is "2", then the file need to be moved to "Folder2" etc..
How can I find the second character of the file name?
Offline
@echo off
setlocal ENABLEDELAYEDEXPANSION
for /f %%i in ('dir /a /b') do (
set filename=%%i
set number=!filename:~1,1!
if [!number!]==[1] move !filename! Folder1\!filename!
if [!number!]==[2] move !filename! Folder2\!filename!
if [!number!]==[3] move !filename! Folder3\!filename!
)
Offline
If you need to handle filenames with spaces:
@echo off
setlocal ENABLEDELAYEDEXPANSION
for /f "delims=;" %%i in ('dir /a /b') do (
set filename=%%i
set number=!filename:~1,1!
if [!number!]==[1] move "!filename!" "Folder1\!filename!"
if [!number!]==[2] move "!filename!" "Folder2\!filename!"
if [!number!]==[3] move "!filename!" "Folder3\!filename!"
)
Offline
Thanks a lot. It worked.
One small Q, what if I need to copy the file into Archive folder, then move into Folder1.
Ex: Suppose if second character of the file name is "1" then need to copy the file into Archive folder, then move it into "Folder1"
Suppose if second character of the file name is "2" then need to copy the file into Archive folder, then move it into "Folder2"
Suppose if second character of the file name is "3" then need to copy the file into Archive folder, then move it into "Folder3"
Suppose if second character of the file name is "4" then need to move it into "Folder4" (Here no need to copy to Archive folder)
Offline
Just make the IF statements multi-line:
if [!number!]==[1] (
copy /y "!filename!" "Archive\!filename!"
move "!filename!" "Folder1\!filename!"
)
Offline
Hi,
I had the following script, but when I ran the script, all the files where the second character is 1 or 2 or 3 are copying into Folder 1 where I expect files where second character is "1" copied to folder1, files where second character is "2" copied to folder2, files where second character is "3" copied to folder3.
Note: The files, Archive folder, Folder4 and Folder5 are on the same server and the Folder1, Folder2, Folder3 are on different server.
@echo off
setlocal EnableDelayedExpansion
PUSHD “Give folder where the files are located”
for /f %%i in ('dir /a /b') do (
set filename=%%i
set number=!filename:~1,1!
if [!number!]==[1] (
copy /y "!filename!" \\Server1\Folder1\OB
move "!filename!" "Archive\!filename!"
)
if [!number!]==[2] (
copy /y "!filename!" \\Server1\Folder2\OB
move "!filename!" "Archive\!filename!"
)
if [!number!]==[3] (
copy /y "!filename!" \\Server1\Folder3\OB
move "!filename!" "Archive\!filename!"
)
if [!number!]==[4] ( move "!filename!" "Folder4\!filename!"
)
if [!number!]==[5] ( move "!filename!" "Folder5\!filename!"
)
Else ( move "!filename!" "Archive\!filename!"
)
)
Offline
Here's a solution that is a little neater to try:
CMD console:
for /L %A in (0,1,9) do copy ?%A* Archive\ && move ?%A* Folder%A\
Batch file:
for /L %%A in (0,1,9) do copy ?%%A* Archive\ && move ?%%A* Folder%%A\
It's generic, and will try to copy files matching ?0* - ?9* to Folder0\ - Folder9\
Errors when a match is not made can be ignored.
If you have folders that might match ?n* that you don't want to mess with, this change would help:
CMD console:
for /L %A in (0,1,9) do copy ?%A*.?* Archive\ && move ?%A*.?* Folder%A\
Batch file:
for /L %%A in (0,1,9) do copy ?%%A*.?* Archive\ && move ?%%A*.?* Folder%%A\
That means it will only copy files with a . extension a minimum of 1 character long.
Now, for copying to different locations, 2 lines instead of one:
CMD console:
for /L %A in (1,1,3) do copy ?%A*.?* Archive\ && move ?%A*.?* \\Server1\Folder%A\OB
for /L %B in (4,1,5) do copy ?%B*.?* Archive\ && move ?%B*.?* Folder%B\
Batch file:
for /L %%A in (1,1,3) do copy ?%%A*.?* Archive\ && move ?%%A*.?* \\Server1\Folder%%A\OB
for /L %%B in (4,1,5) do copy ?%%B*.?* Archive\ && move ?%%B*.?* Folder%%B\
Let the commands themselves do the looping instead of doing it in the script where possible. The compiled commands can do it much faster.
The last solution I gave you will run the copy command 5 times, and the move command 5 times, no matter how many files you are moving. In the solution you have at the moment, they have to run seperately for every file. Your batch file also checks EVERY filename 5 times, copies files 1-3 to the archive twice, copies file 4 to the archive once, and does not copy file 5 to the archive AT ALL.
Offline