0

I have a folder "Projects" and it contains multiple folders for different projects in it, I want to back-up that folder which has some modifications today( it can be single project or can be more than one).

set ProjectFolder=D:\Projects    

for %%o IN (%ProjectFolder%/*.*) DO (
 echo %%o 
 set Project1Folder=%%o
 xcopy "%Project1Folder%\*.*" "%NetworkFolder%\%Project1Folder%\backup_%CurrentDate%\" /s/h/e/k/f/c 
)

And how to pass foldername(modified project) to be be copied in a loop.

Note :- trying something like below but how to get today's modified folder(s) only..

EDIT:-

set MainFolder=D:\Projects          
set LocalFolder=D:\backup

for /d %%D in (%MainFolder%/*.*) do (  
set ProjectFolder=%%~fD
xcopy "%%~fD\*.*" "%LocalFolder%\" /s/h/e/k/f/c /D:03-23-2018
)   

Edit2:-

for /D %%A in ("%LocalFolder%\*") do (
 echo %%~fA
for /F %%N in ('forfiles /S /P %%~fA /M "*" /D +0 /C "cmd /C if @isdir==FALSE echo _" ^| find /C "_"') do set "NUMBER=%%N"
echo %NUMBER%
if %%N GTR 0 xcopy "%%~fA\*.*" "%LocalFolder%\" /s/e/k/f/c
 )
mandeep
  • 21
  • 1
  • 5
  • The path separator in Windows is `\ `(you used `/` once). No need to assign `%%o` to an interim variable, just use `%%o` immediately. If you do want to use the variable you must enable and apply [delayed variable expansion](http://ss64.com/nt/delayedexpansion.html)... – aschipfl Mar 23 '18 at 11:44
  • ok @aschipfl but how do I get folder name?? I think it will give me file names only.. – mandeep Mar 23 '18 at 11:47
  • You have `echo %%o` which shows what `%%o` contains. In your situation it returns `D:\Projects\filename.ext` (given you corrected the path separator). And `%%~dpo` returns the path to the parent directory, for example... Anyway, you should exactly describe what you want, perhaps illustrate it by an example... – aschipfl Mar 23 '18 at 11:51
  • ok let me try but where is today's modified folder, i have missed it.. – mandeep Mar 23 '18 at 11:55
  • hi @aschipfl, I have updated in edit but its not running how to call a for loop in another for loop? – mandeep Mar 23 '18 at 12:15
  • you need [delayed expansion](https://stackoverflow.com/a/30284028/2152082) or simply avoid the unnecessary variable: `for %%o IN (%%~fD/*.*) DO (` – Stephan Mar 23 '18 at 12:18
  • ok @aschipfl its working now but both %%~fD and %%o are showing same folders, I think %%o should list files(or folders) under %%~fD .. – mandeep Mar 23 '18 at 12:25
  • in your EDIT, you *enable* delayed expansion, but you don't *use* it. Replace `%ProjectFolder%` with `!ProjectFolder!` (or avoid it completely, like I suggested before) – Stephan Mar 23 '18 at 12:59
  • its removed @Stephan.. also edit changed now.. – mandeep Mar 23 '18 at 13:11
  • I do still not get what you are trying to achieve. Please precisely explain what should be copied where... – aschipfl Mar 23 '18 at 13:18
  • I am working on multiple projects with code located in a folder "Projects", I randomly work on these projects. I want to take backup of those project(s) whom I worked today. And want this backup on daily basis.. – mandeep Mar 23 '18 at 13:24
  • Edit2 is my final edit, @aschipfl how to use "%%~fA" in another loop(answered by you in another question).. – mandeep Mar 26 '18 at 11:54
  • Ah, you are talking about [this thread](https://stackoverflow.com/q/49485025)... Anyway, you need [delayed expansion](http://ss64.com/nt/delayedexpansion.html) for variable `NUMBER` or you use `%%N` immediately, given that the `if %%N gtr 0 xcopy ...` command line is moved into the inner `for /F` loop... – aschipfl Mar 26 '18 at 12:01
  • yes @aschipfl, but I am getting echo off in this line :- for /F %%N in ('forfiles /S /P %%~fA /M "*" /D +0 /C "cmd /C if @isdir==FALSE echo _" ^| find /C "_"') do set "NUMBER=%%N" echo %NUMBER% – mandeep Mar 26 '18 at 12:09
  • I think its not taking %%~fA .. – mandeep Mar 26 '18 at 12:09
  • Put quotes around like `"%%~fA"`. `ECHO is off.` is returned as delayed expansion is missing and therefore `%NUMBER%` appears empty to the `echo` command... – aschipfl Mar 26 '18 at 12:12
  • already tried same echo off.. – mandeep Mar 26 '18 at 12:16
  • even xcopy "%%~fA\*.*" "%LocalFolder%\" /s/e/k/f/c is working.. – mandeep Mar 26 '18 at 12:19

0 Answers0