[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Batch file - Search "Source" from unknown locations, Check status of "Destination" then Copy "Source" to "Destination" Rename files after copied.

Posted on 2010-01-06
14
Medium Priority
?
446 Views
Last Modified: 2012-05-08
Hi all Experts,

I need a batch file to run the following jobs. "As long as the Source is not modify .."

Thanks in advance ..

set Source:       "My Secret Folder"

set Destination: "%systemdrive%\WINDOWS\system32\My Secret Folder"

1. Check if "Destination" is not exist, creates it.

2. Then Search all drives, with "\My Secret Folder", Exclude drive C:

3. If found "Source" in any drives, stop searching further

4. Then copy all files, including Sub-directories in Source to Destination.

5. ECHO If any files or Sub-Directories were copied to Destination.

6. Rename all files after the copied process is done as the below example:

    Original file:       JPG, The File you just copied From Source to Destination 1

    After Renamed: PIC.The.File.You.Just.Copied.From.Source.To.Destination.1

Else if

1. Destination is exists, then compare the the "Source" and "Destination"

2. Copy any new files or Sub-Folders from Source to Destination.

3. ECHO If any new files or Sub-Directories were copied to Destination.

4. Repeat as step 6 above.
////////////////////////////////////////////////////////

@echo off

setlocal

set Source="D:\My Secret Folder" (<-------- Drive D, I'm testing, Please ignore this ..).

set Destination="%systemdrive%\WINDOWS\system32\My Secret Folder"

if exist "%Destination%" ECHO %Destination% is exist

if not exist "%Destination%" mkdir "%Destination%"

xcopy "%Source%" "%Destination%\" /E /V /C /H /R /Y

PAUSE ..
0
Comment
Question by:cunoc
  • 8
  • 6
14 Comments
 
LVL 71

Expert Comment

by:Qlemo
ID: 26195187
Some questions:
  1. Can you elaborate on what you mean with "As long as the Source is not modify .."?
  2. Can we assume a fixed set of drive letters to use, or needs it to be dynamic?
  3. In step 6, JPG and PIC are no file extensions? I would have expected the file names to be more like      One of many possible file names.JPG.
  4. It will be much easier if we can copy file by file, checking if it is newer if existing (xcopy does that already for us). That is since we have to implement the file name conversion twice else.

0
 

Author Comment

by:cunoc
ID: 26195228
Hi Qlemo,

What i mean is that I want to use any command, as long as

copy "source" --> "Destination",     not move "source" --> "Destination",

Rename the files and Sub-directory only in "Destination" not in the "Source"

Thanks
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 26195235
That answers the first question. What about the other 3?
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

Author Comment

by:cunoc
ID: 26195237
Hi Qlemo,

JPG, File 1.jpg  --> PIC.File1.jpg
0
 

Author Comment

by:cunoc
ID: 26195240
The drive is Dynamic...
0
 

Author Comment

by:cunoc
ID: 26195253
Q. It will be much easier if we can copy file by file, checking if it is newer if existing (xcopy does that already for us). That is since we have to implement the file name conversion twice else.

A. Whatever that make it easy for you...

Thanks
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 26195771
Here is my (untested) draft version. It prompts for the source and destination folder. Since I have not tested, I cannot guarantee it works.

@echo off
setlocal EnableDelayedExpansion

set /P Source=What is the source folder? 
set /P dest=Where to put it to? 

if not defined Source echo No source folder provided. Exit. & exit /b
if not defined Dest   echo No destination folder provided. Exit. & exit /b

REM Determine which drives we have
set drives=
for /F "Tokens=1*" %%A in (
    'fsutil fsinfo drives ^| more'
  ) do if "%%B" == "" (set drives=!drives! %%A)  else (set drives=!drives! %%B)
set drives=%drives:~1%
set drives=%drive:C:\=%

REM We should have all drives, except C, as   D:\ E:\ F:\    aso.

set loc=
for %%A in (%drives%) do if not defined loc if exist %%A%source% set loc=%%A
if not defined loc echo Source folder not found. Exit. & exit /b

REM Now we have our source drive ...

REM Brute Force creation of target folder. Error message will be dismissed.
md %dest% 2>nul

if not "%dest:~-1%" == "\" set dest=%dest%\
set anyfiles=
for /F "tokens=*" %%A in ('dir /s /b %loc%%source%\*') do (
  set anyfiles=y
  set newname=%%~A
  set newname=!newname: =!
  set newname=!newname:JPG,=PIC.!
  xcopy "%%~A" "%dest%%newname%"
)
if defined anyfiles echo Some files have been copied.
pause

Open in new window

0
 

Author Comment

by:cunoc
ID: 26195966
Hi Qlemo,
I tested it and it doesn't run.
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 26196128
Sorry, several mistakes, and something completely unexpected with the xcopy behaviour ...

@echo off
setlocal EnableDelayedExpansion

set /P Source=What is the source folder? 
set /P dest=Where to put it to? 


if not defined Source echo No source folder provided. Exit. & exit /b
if not defined Dest   echo No destination folder provided. Exit. & exit /b

REM Determine which drives we have
set drives=
for /F "Tokens=1*" %%A in (
    'fsutil fsinfo drives ^| more'
  ) do if "%%B" == "" (set drives=!drives! %%A)  else (set drives=!drives! %%B)
set drives=%drives:~1%
set drives=%drive:C:\=%

REM We should have all drives, except C, as   D:\ E:\ F:\    aso.
if     "%source:~0,1%" == "\"  set source=%source:~1%
if not "%source:~-1%"  == "\"  set source=%source%\
if not "%dest:~-1%"    == "\"  set dest=%dest%\

set loc=
for %%A in (%drives%) do if not defined loc if exist %%A%source% set loc=%%A
if not defined loc echo Source folder not found. Exit. & exit /b

REM Now we have our source drive ...

REM Brute Force creation of target folder. Error message will be dismissed.
md %dest% 2>nul

set anyfiles=
for /F "tokens=*" %%A in ('dir /s/b/A:-d %loc%%source%*') do (
  set anyfiles=y
  set newname=%%~nxA
  set newname=!newname: =!
  set newname=!newname:JPG,=PIC.!
  set newpath=%%~pA
  set newpath=!newpath:\%source%=!
  md "%dest%!newpath!" 2>nul
  echo F| xcopy "%%~A" "%dest%!newpath!!newname!" /C/V/R/H/D/Y/F 2>nul
)
if defined anyfiles echo Some files have been copied.
pause

Open in new window

0
 

Author Comment

by:cunoc
ID: 26197996
Hi Qlemo,
For some reasons, the script doesn't work at all, Thanks

set Source=\MySecretFolder (<----In the final script, Assume I don't know where this folder is located.)
set dest=C:\MySecretFolder

A subdirectory or file C:\MySecretFolder\ already exists.
Error occurred while processing: C:\MySecretFolder\.
A subdirectory or file 2 already exists.
Error occurred while processing: 2.
File Not Found

/////////////////////////////////////////

set Source=M:\MySecretFolder (<---- For testing, I know where it located.)
set dest=C:\MySecretFolder

A subdirectory or file C:\MySecretFolder\ already exists.
Error occurred while processing: C:\MySecretFolder\.
A subdirectory or file 2 already exists.
Error occurred while processing: 2.
Invalid number of parameters
0 File(s) copied
Invalid number of parameters
0 File(s) copied
Invalid number of parameters
0 File(s) copied
Invalid number of parameters
0 File(s) copied
Invalid number of parameters
0 File(s) copied
Invalid number of parameters
0 File(s) copied
The process tried to write to a nonexistent pipe.
Invalid number of parameters
0 File(s) copied

///////////////////////////////////

@echo off
setlocal EnableDelayedExpansion

::set Source="M:\MySecretFolder"
::set dest="C:\MySecretFolder"

::set Source="MySecretFolder"
::set dest="C:\MySecretFolder"

::set Source="\My Secret Folder"
::set dest="C:\My Secret Folder"
 
 
if not defined Source echo No source folder provided. Exit. & exit /b
if not defined Dest   echo No destination folder provided. Exit. & exit /b
 
REM Determine which drives we have
set drives=
for /F "Tokens=1*" %%A in (
    'fsutil fsinfo drives ^| more'
  ) do if "%%B" == "" (set drives=!drives! %%A)  else (set drives=!drives! %%B)
set drives=%drives:~1%
set drives=%drive:C:\=%
 
REM We should have all drives, except C, as   D:\ E:\ F:\    aso.
if     "%source:~0,1%" == "\"  set source=%source:~1%
if not "%source:~-1%"  == "\"  set source=%source%\
if not "%dest:~-1%"    == "\"  set dest=%dest%\
 
set loc=
for %%A in (%drives%) do if not defined loc if exist %%A%source% set loc=%%A
if not defined loc echo Source folder not found. Exit. & exit /b
 
REM Now we have our source drive ...
 
REM Brute Force creation of target folder. Error message will be dismissed.
md %dest% 2
 
set anyfiles=
for /F "tokens=*" %%A in ('dir /s/b/A:-d %loc%%source%*') do (
  set anyfiles=y
  set newname=%%~nxA
  set newname=!newname: =!
  set newname=!newname:JPG,=PIC.!
  set newpath=%%~pA
  set newpath=!newpath:\%source%=!
  md "%dest%!newpath!" 2>nul
  echo F| xcopy "%%~A" "%dest%!newpath!!newname!" /C/V/R/H/D/Y/F 2
)
if defined anyfiles echo Some files have been copied.
pause ..

pause ..
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 26202773
You have lost the >nul with each 2>nul, hence you get those "2" errors, and the incorrect parameter count.

Because of that, you will have a folder "2" created somewhere.
0
 

Author Comment

by:cunoc
ID: 26203835
Hi Qlemo,
I run the script that you provided, I modify nothing, just only the set /P Source=N:\Temp0001\ and set /P dest=C:\Temp0002\ and the script doesn't run at all.

@echo off
setlocal EnableDelayedExpansion
 
set /P Source=N:\Temp0001\
set /P dest=C:\Temp0002\
 
 
if not defined Source echo No source folder provided. Exit. & exit /b
if not defined Dest   echo No destination folder provided. Exit. & exit /b
 
REM Determine which drives we have
set drives=
for /F "Tokens=1*" %%A in (
    'fsutil fsinfo drives ^| more'
  ) do if "%%B" == "" (set drives=!drives! %%A)  else (set drives=!drives! %%B)
set drives=%drives:~1%
set drives=%drive:C:\=%
 
REM We should have all drives, except C, as   D:\ E:\ F:\    aso.
if     "%source:~0,1%" == "\"  set source=%source:~1%
if not "%source:~-1%"  == "\"  set source=%source%\
if not "%dest:~-1%"    == "\"  set dest=%dest%\
 
set loc=
for %%A in (%drives%) do if not defined loc if exist %%A%source% set loc=%%A
if not defined loc echo Source folder not found. Exit. & exit /b
 
REM Now we have our source drive ...
 
REM Brute Force creation of target folder. Error message will be dismissed.
md %dest% 2>nul
 
set anyfiles=
for /F "tokens=*" %%A in ('dir /s/b/A:-d %loc%%source%*') do (
  set anyfiles=y
  set newname=%%~nxA
  set newname=!newname: =!
  set newname=!newname:JPG,=PIC.!
  set newpath=%%~pA
  set newpath=!newpath:\%source%=!
  md "%dest%!newpath!" 2>nul
  echo F| xcopy "%%~A" "%dest%!newpath!!newname!" /C/V/R/H/D/Y/F 2>nul
)
if defined anyfiles echo Some files have been copied.
pause

0
 
LVL 71

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 26204572
If you do not want prompts for paths, you need to remove the /P, too, so it is:

set Source=N:\Temp0001\
set dest=C:\Temp0002\

Should it not run after changing that, remove the @echo off (first line). That should echo (most) commands as they are executed.
0
 

Author Closing Comment

by:cunoc
ID: 31673648
Hi Qlemo,

The script you provided is working great. That is what I was waiting for.

Thanks..
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
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…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Loops Section Overview
Suggested Courses
Course of the Month19 days, 13 hours left to enroll

873 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