Solved

Batch file - xcopy - change files name if already present

Posted on 2011-03-02
12
523 Views
Last Modified: 2012-05-11
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

Open in new window

code-screenshot.jpg
0
Comment
Question by:IT_newbie01
  • 5
  • 5
  • 2
12 Comments
 
LVL 68

Expert Comment

by:Qlemo
ID: 35019672
That doesn't make much sense, as we cannot see how you want the folder name being added to the file name.
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.
0
 

Author Comment

by:IT_newbie01
ID: 35021132
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??
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 35021403
Let's see.
@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
)

Open in new window

That will copy e.g.   c:\pst_files\a\b\outlook.pst   to   c:\test\outlook.a.pst .
0
 

Author Comment

by:IT_newbie01
ID: 35022796
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.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 35022898
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
)

Open in new window

0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 35022968
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
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 68

Expert Comment

by:Qlemo
ID: 35022992
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.
0
 

Author Comment

by:IT_newbie01
ID: 35023053
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
0
 

Author Comment

by:IT_newbie01
ID: 35023064
I meant

ex:

some_folder
    some_folder1
          user_folder2
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 300 total points
ID: 35023094
the last folder before the root folder
I thought you wanted it like that.
@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:~0,-1!
   for %%D in ("!folder!") do set folder=%%~nxD
   copy "%%~F" "%dst%\%%~nF.!folder!%%~xF
)

Open in new window

0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 35023184
Points to Qlemo on this question, he's been working it with you, but I think this may do what you want.

@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"
   )
)

Open in new window


~bp
0
 

Author Closing Comment

by:IT_newbie01
ID: 35023221
Sorry for the confusion!  Works great thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now