new_to_networks
asked on
Sorting files by folder with .cmd line.
What would I put for a .cmd script where I want to move all the files from one directory to another, but when they reach their destination they are all sorted out in folders by date created? So they're all just together in one folder, but when I move them to the archived folder, there will be subfolders that contain them by date. So in the new folder, there would be subfolders that look like:
6/1/11
6/2/11
6/3/11
And they would contain the files from those dates. The files themselves are actually named by date. So a file is named 20110601, 20110602. I know how to move files and create directories from the cmd line, but I don't know how to tell it to create all the folders needed to sort these files by their individual dates and then how to send the files to their respective subfolders. Any help would be much appreciated.
6/1/11
6/2/11
6/3/11
And they would contain the files from those dates. The files themselves are actually named by date. So a file is named 20110601, 20110602. I know how to move files and create directories from the cmd line, but I don't know how to tell it to create all the folders needed to sort these files by their individual dates and then how to send the files to their respective subfolders. Any help would be much appreciated.
ASKER
That sounds right, yes. All the 20110601 files need to go into a 6-1-11 folder. All the 20110602 files need to go to a 6-2-11 folder. There's no chance of duplicate file names. And yes, all these folders would need to be created with .cmd script as they currently do not exist.
ASKER
They were created on that same date as well, so if they could be sorted by date created/modified into folders with those dates, that would be the solution as well.
Can you give us some sample names? (All possible combinations)
The files will always start in the same folder and be moved to respective (dated) folders?
Do you want the dated folders as subfolders of the existing or another location entirely?
The files will always start in the same folder and be moved to respective (dated) folders?
Do you want the dated folders as subfolders of the existing or another location entirely?
ASKER
20110613-070706-HEI_623555 1234-18005 551234.wav
20111228-090021-John_Doe_5 115-5115.w av
20111228-123415-HEI_5120-1 8885553095 .wav
20110906-123421-+180555512 34_+180555 2345-7282. wav
They all start with the date and then go into numbers dialed, names, and extensions. These are recorded phone calls going in and out from certain departments. Hard to see all possible variations but they all start with the date and all end in .wav. They will always be pulled from the same folder and moved to their own dated folder in its own directory. Example: c:/>storage1\salesrecordin gs\06-01-1 1.
20111228-090021-John_Doe_5
20111228-123415-HEI_5120-1
20110906-123421-+180555512
They all start with the date and then go into numbers dialed, names, and extensions. These are recorded phone calls going in and out from certain departments. Hard to see all possible variations but they all start with the date and all end in .wav. They will always be pulled from the same folder and moved to their own dated folder in its own directory. Example: c:/>storage1\salesrecordin
You could do something like this
#!/bin/bash
DIR="$(echo 20110601 | awk '{y=substr($1,3,2);m=substr($1,5,2);d=substr($1,7,2); printf "/var/tmp/%s/%s/%s",m,d,y;}')"
if [ ! -e "${DIR}" ]; then
mkdir -p "${DIR}"
fi
cp 20110601 ${DIR}
chris:/var/tmp
$ ls -l /var/tmp/06/01/11/
total 0
-rw-r--r-- 1 chris wheel 0 Feb 14 19:29 20110601
Ah or maybe this
$ ls -l 12-28-11/
total 0
-rw-r--r-- 1 chris wheel 0 Feb 14 19:33 20111228-090021-John_Doe_5 115-5115.w av
$ cat script.sh
#!/bin/bash
DIR="$(echo 20111228-090021-John_Doe_5115-5115.wav | awk -F'-' '{y=substr($1,3,2);m=substr($1,5,2);d=substr($1,7,2); printf "/var/tmp/%s-%s-%s",m,d,y;}')"
if [ ! -e "${DIR}" ]; then
mkdir -p "${DIR}"
fi
$ ls -l 12-28-11/
total 0
-rw-r--r-- 1 chris wheel 0 Feb 14 19:33 20111228-090021-John_Doe_5
Try this:
@echo off
setlocal ENABLEDELAYEDEXPANSION
for %%a in (*.wav) do (
set name=%%a
set num1=!name:~0,4!
set num2=!name:~4,2!
set num3=!name:~6,2!
set name2=!num1!-!num2!-!num3!
if not exist c:\storage1\salesrecordings\!name2! mkdir c:\storage1\salesrecordings\!name2!
move !name! c:\storage1\salesrecordings\!name2!
)
ASKER
Where do I put the source folder in that one?
Like if the source folder is C:>phonerecordings\ and the destination folder is C:>storage1\salesrecording s\.
Is that what the bottom two entires are at the bottom? Source folder and destination folder?
Like if the source folder is C:>phonerecordings\ and the destination folder is C:>storage1\salesrecording
Is that what the bottom two entires are at the bottom? Source folder and destination folder?
No - the %%a is a variable that represents the source folder. That program is designed to run from the command line.
Say you save it as movedated.bat you would then run it from the command line:
movedated c:\phonerecordings
If you want to set it in the program you could add a line:
set a = c:\phonerecordings
Just before the "for" line.
Say you save it as movedated.bat you would then run it from the command line:
movedated c:\phonerecordings
If you want to set it in the program you could add a line:
set a = c:\phonerecordings
Just before the "for" line.
ASKER
I'm sorry, I must be doing something wrong. I ran a test with this using .txt files named the same as .wav files and tried moving them from a test2 folder to a test3 folder. This is what happened. Maybe you can see what I'm not getting.
c:\>@echo off
setlocal ENABLEDELAYEDEXPANSION
for c:\>test2 in (*.txt) do (
c:\ was unexpected at this time.
set name=c:\>test2
Access is denied.
set num1=!name:~0,4!
set num2=!name:~4,2!
set num3=!name:~6,2!
set name2=!num1!-!num2!-!num3!
if not exist c:\test3\!name2! mkdir c:\test3\!name2!
move !name! c:\test3\!name2!
The system cannot find the file specified.
)
c:\>@echo off
setlocal ENABLEDELAYEDEXPANSION
for c:\>test2 in (*.txt) do (
c:\ was unexpected at this time.
set name=c:\>test2
Access is denied.
set num1=!name:~0,4!
set num2=!name:~4,2!
set num3=!name:~6,2!
set name2=!num1!-!num2!-!num3!
if not exist c:\test3\!name2! mkdir c:\test3\!name2!
move !name! c:\test3\!name2!
The system cannot find the file specified.
)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here should be a full working program for you complete with comments. I would test it first if you can though:
@echo off
setlocal ENABLEDELAYEDEXPANSION
REM ***************************************************
REM * Change to the C: drive *
REM * (in case your command prompt defaults elsewhere *
REM ***************************************************
cd c:
REM ***************************************************
REM * change to the default (source) folder *
REM ***************************************************
cd phonerecordings
REM ***************************************************
REM * Read the filenames one at a time and extract *
REM * the first 8 characters (the date) *
REM * Break down the date into the individual parts *
REM * (Year, Month and day) in 3 variables *
REM ***************************************************
for %%a in (*.wav) do (
set name=%%a
set num1=!name:~0,4!
set num2=!name:~4,2!
set num3=!name:~6,2!
REM ***************************************************
REM * make a variable for the folder name in the *
REM * format 2012-03-13 *
REM ***************************************************
set name2=!num1!-!num2!-!num3!
REM ***************************************************
REM * Check for existance of folder and create if not *
REM * there otherwise just move the file into the *
REM * folder *
REM ***************************************************
if not exist c:\storage1\salesrecordings\!name2! mkdir c:\storage1\salesrecordings\!name2!
move !name! c:\storage1\salesrecordings\!name2!
)
ASKER
Awesome. That's very helpful.
Glad to be of help.
1. Create a folder
2. Read file names and extract date portion (i.e. 20110601file.txt > 20110601) to variable
2. Create subfolder named for date variable (i.e. 20110601)
3. Move all files with 20110601 as part of the name to the folder created in step 2
4. Repeat for every date found as part of a filename
Is that what you are wanting to do?
Will there be any any chance of duplicate file names?
And I would advise against having slashes (/) in the folder name.