IT_newbie01
asked on
Batch file - xcopy - change files name if already present
Hey experts, im looking to modify or find a better solution to code to copy .pst files to another location and grab the folder name of the last parent folder and add that to the file name if thefile already exist in that location
files
code-screenshot.jpg
ASKER
As the code is I search 1 drive of multiple directories and sub directories and copy those files into 1 folder. "test". However this creates a files permissions errors since there different files with the same name in different folders. However the last folder before the root folder are different so I want to grab that folder name and append it to the file name so there unique. Make sense??
Let's see.
st to c:\test\outlook.a.pst .
@echo off
setlocal EnableDelayedExpansion
set src=C:\pst_files
set dst=c:\test
for /F "tokens=*" %%F in ('dir /s/b/a:-d %src%\*.pst') do (
set folder=%%dpF
set folder=!folder:%src%\=!
for /F "tokens=1 delims=\" %%D in ("!folder!") do set folder=%%D
copy "%%~F" "%dst%\%%nF.!folder!%%xF
)
That will copy e.g. c:\pst_files\a\b\outlook.pASKER
Ok, the code appears to only copy 1 file (im assuming the duplicate file); it changes the file type and labels the file "%nf.%dpfxf". I also want to copy the all the .pst files (including the duplicates).
Thanks for your help.
Thanks for your help.
Sorry, I'm stupid, forgetting the ~ each time. Try this:
@echo off
setlocal EnableDelayedExpansion
set src=C:\pst_files
set dst=c:\test
for /F "tokens=*" %%F in ('dir /s/b/a:-d %src%\*.pst') do (
set folder=%%~dpF
set folder=!folder:%src%\=!
for /F "tokens=1 delims=\" %%D in ("!folder!") do set folder=%%~D
copy "%%~F" "%dst%\%%~nF.!folder!%%~xF
)
Can you clarify the naming of the destination files a little more? For example, if you have:
c:\pst_files\dir1\mail.pst
c:\pst_files\dir2\mail.pst
should those be copied as:
c:\pst_files\dir1\mail.pst ==> c:\test\mail.pst
c:\pst_files\dir2\mail.pst ==> c:\test\dir2_mail.pst
Or am I missing something?
~bp
c:\pst_files\dir1\mail.pst
c:\pst_files\dir2\mail.pst
should those be copied as:
c:\pst_files\dir1\mail.pst
c:\pst_files\dir2\mail.pst
Or am I missing something?
~bp
I have interpreted it almost like you: First occurance should be copied as-is, each conflicting should be copied with the topmost folder name added (appended or prepended is not defined yet).
However, I expanded that to that the topmost folder name always is appended.
However, I expanded that to that the topmost folder name always is appended.
ASKER
Ok, that works much better! However it's not grabbing the last folder; but the 1st folder name of sub-directory for that file.
ex:
some_file
some_file1
user_file2
currently gives some_file.pst instead of user_file2.pst
ex:
some_file
some_file1
user_file2
currently gives some_file.pst instead of user_file2.pst
ASKER
I meant
ex:
some_folder
some_folder1
user_folder2
ex:
some_folder
some_folder1
user_folder2
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Points to Qlemo on this question, he's been working it with you, but I think this may do what you want.
~bp
@echo off
set src=c:\pst_files
set dst=c:\test
for /F "tokens=*" %%F in ('dir /s/b/a:-d %src%\*.pst') do (
if not exist "%dst%\%%~nxF" (
copy "%%~F" "%dst%\"
) else (
for %%D in ("%%~pF\.") do copy "%%~F" "%dst%\%%~nD_%%~nxF"
)
)
~bp
ASKER
Sorry for the confusion! Works great thanks!
As far as I understood you want the folder name only if the file already exists. Again, that does not make much sense to me.
Maybe you should tell us more about what you REALLY want to achieve, so we can provide better help HOW to do it.