Make directories from file name and move file

September 16, 2009 at 09:22:47
Specs: Windows XP

I have a directory set up that stores *.CSV files.
I need to read the file name of the file, create a directory with the same file name, move the *.csv file into the new directory and rename the file to the date and time.CSV

See More: Make directories from file name and move file

Report •

September 16, 2009 at 15:04:11
rename the file to the date and time.CSV

Do you want the current date/time or the file creation date/time?

Report •

September 17, 2009 at 10:05:07

Yes I do. I have found this that nearly fits the bill

for /f %%F in ('dir/b/a-d *.csv') do call :sub1 %%F
goto :eof

set name=%1
md %name:~0,6%
move %* %name:~0,6%

but only looks for a specific string length of file name rather than *.CSV.

Can you help please?

Report •

September 17, 2009 at 19:50:43
You didn't answer my query on date/time so I've used the file created date and time.

Without knowing your date/time format the script is set up for my format which is dd-mm-yyyy hh:mm. You will have to change

set file=!file:~6,4!!file:~3,2!!file:~0,2!_!file:~11,2!!file:~-2!.csv
to suit your format if it is different. The output files are named in the format yyyymmdd_hhmm. Once you have finished testing change the "Copy" command to "Move". The script is untested.

@echo off
setlocal enabledelayedexpansion

for /f "delims=" %%A in ('dir /a-d /b /tc "*.csv"') do (
    md "%%~nA"

    set file=%%~tA

    set file=!file:~6,4!!file:~3,2!!file:~0,2!_!file:~11,2!!file:~-2!.csv

    copy "%%A" "%%~nA\!file!"> nul

Good luck

Report •

Related Solutions

September 19, 2009 at 07:55:26
Sorry about not answering your question properly.

Your script works as indicated but I have changed the final date and time format of the CSV file.

Many thanks for your time to answer my question.

Report •

Ask Question