Link to home
Start Free TrialLog in
Avatar of cunoc
cunoc

asked on

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 ..
Avatar of Qlemo
Qlemo
Flag of Germany image

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.

Avatar of cunoc
cunoc

ASKER

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
That answers the first question. What about the other 3?
Avatar of cunoc

ASKER

Hi Qlemo,

JPG, File 1.jpg  --> PIC.File1.jpg
Avatar of cunoc

ASKER

The drive is Dynamic...
Avatar of cunoc

ASKER

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
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

Avatar of cunoc

ASKER

Hi Qlemo,
I tested it and it doesn't run.
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

Avatar of cunoc

ASKER

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 ..
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.
Avatar of cunoc

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cunoc

ASKER

Hi Qlemo,

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

Thanks..