Computing.Net > Forums > Programming > Make directories from file name and move file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

Make directories from file name and move file

Reply to Message Icon

Name: trekman
Date: September 16, 2009 at 09:22:47 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

Hi,

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



Sponsored Link
Ads by Google

Response Number 1
Name: Wahine
Date: September 16, 2009 at 15:04:11 Pacific
Reply:

rename the file to the date and time.CSV

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


0

Response Number 2
Name: trekman
Date: September 17, 2009 at 10:05:07 Pacific
Reply:

Hi,

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

:sub1
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?


0

Response Number 3
Name: Wahine
Date: September 17, 2009 at 19:50:43 Pacific
Reply:

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
cls
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


0

Response Number 4
Name: trekman
Date: September 19, 2009 at 07:55:26 Pacific
Reply:

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.


0
Reply to Message Icon

Related Posts

See More


find and replace string i... Find a line number then c...


Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Make directories from file name and move file

Make new folder from file name www.computing.net/answers/programming/make-new-folder-from-file-name/16189.html

Extract file name from file path www.computing.net/answers/programming/extract-file-name-from-file-path/16973.html

batch - read from file www.computing.net/answers/programming/batch-read-from-file/18208.html