Ryan
asked on
Batch Script to move files conditionally to subfolder in same directory
I have a program that outputs different csv files whos names are 3 digit numbers to a folder (ie 140.csv, 555.csv etc.). I need a script that will, when run, move the files from the main directory to the different subdirectories that I create. Don't need the script to create new folders.
ASKER
No, I have to tell the program to export the data this way, and the next time it exports it will make a directory based on the name of the file I am exporting before it breaks it off. However, I am looking for some conditions to be met to determine which folder to move the file to
This moves all CSVs in a given dir to target dir
Run in a CMD window:
@echo off
set maindir=%1
set tgtdir=%2
for %%a in (%maindir%\???.csv) do (
if exist "%tgtdir%\%%~na\*" (
move "%%~fa" "%tgtdir%\%%~na"
) else (
echo Missing target dir "%tgtdir%\%%~na"
)
)
Run in a CMD window:
MoveCSV "c:\maindir" "c:\targetdir"
ASKER
Thanks for the help, is there a way to say that if the name of the file falls within a specific range to send it to a specific file?
Please clarify via an example
ASKER
The 3 digit numbers refer to first 3 digits of zip codes and I want to separate them by region, so any file named 005.csv (005 coincides with New Jersey), I want to go to my Northeast directory. If the file were named 901.csv (Los Angeles) I want it to go to my West directory.
If you have a comma-delimited .txt file which contains the zip code and State pairs, that should work. e.g. If you have a file Regions.txt
I'd need to revise the .bat code accordingly. Would that work for you?
005,New Jersey
901,Los Angeles
I'd need to revise the .bat code accordingly. Would that work for you?
ASKER
I don't know how that would work. I have different csv files name with different 3 digit combinations. Are you saying that I would need to create a text file for the .bat to read from to determine which directory to move the csv file to?
> I would need to create a text file for the .bat to read from to determine which directory to move the csv file to?
Yes. I assume that would be a one-time thing since the values would not change. Or, would they?
Still, you'd need some kind of database which shows the relations. Especially if there are lots of them.
Yes. I assume that would be a one-time thing since the values would not change. Or, would they?
Still, you'd need some kind of database which shows the relations. Especially if there are lots of them.
ASKER
The values would not change, at least not very often at some point I might want to move a city to a different directory/region
How many do you have?
Actually, Regions.txt needs to be something like this:
This works:
Actually, Regions.txt needs to be something like this:
005,Northeast
901,West
This works:
@echo off
setlocal enabledelayedexpansion
set maindir=%1
set tgtdir=%2
for %%a in (%maindir%\???.csv) do (
REM echo "%%~na,"
for /f "tokens=1-3 delims=," %%A in ('findstr /c:"%%~na," regions.txt') do (
if [!errorlevel!] equ [0] (
REM echo %%A,%%B,%%C
if exist "%tgtdir%\%%C\*" (
echo move "%%~fa" "%tgtdir%\%%C"
move "%%~fa" "%tgtdir%\%%C">nul
) else (
echo Missing target dir "%tgtdir%\%%~na"
)
)
)
)
ASKER
Not really sure how many there are, I guess there could be up to 1000 different file names split to 4 or 5 different regions.
Then you'll need some sort of database, similar to the Regions.txt example.
I don't have that info.
So it needs to be compiled separately. Maybe google luck will land you such a file. Or, you can make it yourself. At this point, I don't have that time right now.
I don't have that info.
So it needs to be compiled separately. Maybe google luck will land you such a file. Or, you can make it yourself. At this point, I don't have that time right now.
ASKER
I appreciate all the help.
Need some clarification here... Your prior comment says:
The 3 digit numbers refer to first 3 digits of zip codes and I want to separate them by region, so any file named 005.csv (005 coincides with New Jersey)
...Which doesn't match the first 3 digits of each NJ here - at least I couldn't find a match: http://www.aip2.com/njzip1.htm
I don't know how accurate the numbers are in that link.
BTW, all the states are here: http://www.paweb2.com/zip.htm
The 3 digit numbers refer to first 3 digits of zip codes and I want to separate them by region, so any file named 005.csv (005 coincides with New Jersey)
...Which doesn't match the first 3 digits of each NJ here - at least I couldn't find a match: http://www.aip2.com/njzip1.htm
I don't know how accurate the numbers are in that link.
BTW, all the states are here: http://www.paweb2.com/zip.htm
@Ryan
Do your regions relate to some industry standard or is this strictly an internal (to your company) assignment?
Do your regions relate to some industry standard or is this strictly an internal (to your company) assignment?
ASKER
The 005 code is associated with the USPS NDC for New Jersey there is a list of the 3 digit codes and which NFC mail goes to on the USPS website.
associating with NJ isn't the issue. You want to associate with some region.
ASKER
It is a internal thing
Then, you'll need to get a list/table/key:value source of these associations. I think that is what the other experts have been trying to communicate with you in this thread.
You can also use FolderAgent without writing any scripts etc.
www.folderagent.com/help/windows_service.html
www.folderagent.com/help/windows_service.html
Shaun... How does your solution handle the zip code issue?
ASKER
NVIT.... you had this code above...
@echo off
setlocal enabledelayedexpansion
set maindir=%1
set tgtdir=%2
for %%a in (%maindir%\???.csv) do (
REM echo "%%~na,"
for /f "tokens=1-3 delims=," %%A in ('findstr /c:"%%~na," regions.txt') do (
if [!errorlevel!] equ [0] (
REM echo %%A,%%B,%%C
if exist "%tgtdir%\%%C\*" (
echo move "%%~fa" "%tgtdir%\%%C"
move "%%~fa" "%tgtdir%\%%C">nul
) else (
echo Missing target dir "%tgtdir%\%%~na"
)
)
)
)
What do I need to do to be able to use this. I am new to this and want to make I understand what I am doing so if I need to modify I am able to do that.
Attached is the regions.txt I created.
regions.txt
@echo off
setlocal enabledelayedexpansion
set maindir=%1
set tgtdir=%2
for %%a in (%maindir%\???.csv) do (
REM echo "%%~na,"
for /f "tokens=1-3 delims=," %%A in ('findstr /c:"%%~na," regions.txt') do (
if [!errorlevel!] equ [0] (
REM echo %%A,%%B,%%C
if exist "%tgtdir%\%%C\*" (
echo move "%%~fa" "%tgtdir%\%%C"
move "%%~fa" "%tgtdir%\%%C">nul
) else (
echo Missing target dir "%tgtdir%\%%~na"
)
)
)
)
What do I need to do to be able to use this. I am new to this and want to make I understand what I am doing so if I need to modify I am able to do that.
Attached is the regions.txt I created.
regions.txt
@NVIT: The start of the filename
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@Shaun.
...and how does it handle the associated region issue?
...and how does it handle the associated region issue?
Open in new window
Run in a CMD window:
Open in new window
Are duplicates on the target a concern?