Avatar of Ryan
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.
PowershellWindows BatchScripting Languages

Avatar of undefined
Last Comment
Shaun Vermaak

8/22/2022 - Mon
NVIT

For a .bat version, make file MoveCSV.bat:

set maindir=%1
set tgtdir=%2
move /y "%maindir%\???.csv" "%tgtdir%"

Open in new window


Run in a CMD window:
MoveCSV "c:\maindir" "c:\targetdir"

Open in new window


Are duplicates on the target a concern?
Ryan

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
NVIT

This moves all CSVs in a given dir to target dir
@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"
  )
)

Open in new window


Run in a CMD window:
MoveCSV "c:\maindir" "c:\targetdir"

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Ryan

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?
NVIT

Please clarify via an example
Ryan

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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
NVIT

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
005,New Jersey
901,Los Angeles

Open in new window


I'd need to revise the .bat code accordingly. Would that work for you?
Ryan

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?
NVIT

> 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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Ryan

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
NVIT

How many do you have?

Actually, Regions.txt needs to be something like this:
005,Northeast 
901,West

Open in new window


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"
      )
    )
  )
)

Open in new window

Ryan

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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
NVIT

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.
Ryan

ASKER
I appreciate all the help.
NVIT

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
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
aikimark

@Ryan

Do your regions relate to some industry standard or is this strictly an internal (to your company) assignment?
Ryan

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.
aikimark

associating with NJ isn't the issue.  You want to associate with some region.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Ryan

ASKER
It is a internal thing
aikimark

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.
Shaun Vermaak

You can also use FolderAgent without writing any scripts etc.
www.folderagent.com/help/windows_service.html
foldera1.jpg
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
NVIT

Shaun... How does your solution handle the zip code issue?
Ryan

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
Shaun Vermaak

@NVIT: The start of the filename
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
NVIT

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
NVIT

@Shaun.
...and how does it handle the associated region issue?
Shaun Vermaak

With a lookup, same as ID: 42093768
Can even extend it with a few lines of Powershell instead of writing the whole thing from scratch. It runs as a service so you don't need to schedule scripts etc.
foldera2.jpg