1.

Solve : Create folder using part filename and move?

Answer»

Hello All,

I am hoping someone can HELP me out.

I am trying to sort out how to create a .BAT file to automate the folder creation according to a PART of a filename (excluding a certain word), move files to that newly created folder, and then append the date modified to the end of the files within the new folder.

For example:

UserName.PST
UserName-Archive.PST
DifferentUser.PST
DifferentUser-Archive.PST

The .BAT file should create a folder according to the *username but excludes creating a folder for the PST file with "-Archive" within the filename. So in the above scenario, I would end up with a "UserName" folder, as well as a "DifferentUser" folder.
The files would then be moved to the appropriate folder.
After the move, all files within that folder would then have the date appended to the end of the file.

Below is the command I have been working with so far. It works well for users without "-Archive" in the name, but doesn't do as intended when "-Archive" is included in the file name.

Code: [Select]@echo off
for %%a in (*.PST) do (md "%%~Na" && move "%%a" "%%~Na" && ren %%~Na\*.PST ?????????????????????????????????????????????_%date:~-10,2%%date:~-7,2%%date:~-4,4%.PST)
I hope I have given enough information. Please ask if more is required.

Thank you for your assistance.I have found a solution of sorts. I used the below code to do what I needed, however, it appears to add the date twice only to the end of the filenames with "-Archive" in it.
So:

UserName.PST appears as UserName_04042016.PST
UserName-Archive.PST appears as UserName-Archive_04042016_04042016.PST

Code: [Select]@echo off
setlocal enabledelayedexpansion
for %%A in (*.pst) do (
echo file found %%A
for /f "delims=" %%B in ("%%A") do set fname=%%~nB
for /f "delims=" %%C in ("%%A") do set fextn=%%~xC
for /f "tokens=1* delims=-" %%D in ("!fname!") do set folname=%%D
echo folder name !folname!
if not EXIST "!folname!" (
echo Folder !folname! does not exist, creating
md "!folname!"
) else (
echo Folder !folname! EXISTS
)
echo Moving file %%A to folder !folname!
move "%%A" "!folname!"
ren !folname!\*.PST ???????????????????????????????????????????_%date:~-10,2%%date:~-7,2%%date:~-4,4%.PST
)

Now I need assistance to remove the double up of the appended date.

Thank you.All working. I found my mistake regarding the double up of the date being added to files with "-Archive" in the name.
Instead of USING ren "!folname!\*.PST" , I changed it to be ren "!folname!\%%A"

Below is the code I ended up using:

Code: [Select]@echo off
setlocal enabledelayedexpansion
for %%A in (*.PST) do (
echo file found %%A
for /f "delims=" %%B in ("%%A") do set fname=%%~nB
for /f "delims=" %%C in ("%%A") do set fextn=%%~xC
for /f "tokens=1* delims=-" %%D in ("!fname!") do set folname=%%D
echo folder name !folname!
if not exist "!folname!" (
echo Folder !folname! does not exist, creating
md "!folname!"
) else (
echo Folder !folname! exists
)
echo Moving file %%A to folder !folname!
move "%%A" "!folname!"
ren "!folname!\%%A" "?????????????????????????????????_%date:~-10,2%%date:~-7,2%%date:~-4,4%.PST"
)
Quote from: StingerMeGood on April 03, 2016, 09:06:39 PM

move files to that newly created folder, and then append the date modified to the end of the files within the new folder.

You are using the current date in your code, yet you asked above for date modified.


The code below should create folders for the username and then move the two .pst files to the folder.
It doesn't do the date requirement - it's not clear as yet.

Code: [Select]@echo off
for /f "delims=" %%a in ('dir *.pst /b /a-d ^|find /i /v "-archive" ') do md "%%~na" & move "%%a" "%%~na" & move "%%~na-archive%%~XA" "%%~na"


Discussion

No Comment Found