• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 449
  • Last Modified:

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

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
cunoc
Asked:
cunoc
  • 8
  • 6
1 Solution
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
 
cunocAuthor Commented:
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
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
That answers the first question. What about the other 3?
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
cunocAuthor Commented:
Hi Qlemo,

JPG, File 1.jpg  --> PIC.File1.jpg
0
 
cunocAuthor Commented:
The drive is Dynamic...
0
 
cunocAuthor Commented:
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
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
 
cunocAuthor Commented:
Hi Qlemo,
I tested it and it doesn't run.
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
 
cunocAuthor Commented:
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
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
 
cunocAuthor Commented:
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
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
 
cunocAuthor Commented:
Hi Qlemo,

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

Thanks..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now