batch file

Discussion in 'Software' started by medic42, Jul 21, 2005.

  1. medic42

    medic42 Private E-2

    I'm wanting my batch file to copy files and folders from c:\act to a network drive E:\ACT then change the name of E:\ACT to include the date. Also this is not copying the subdirectories. Can someone help me?

    @echo off
    cd c:\ACT
    copy c:\ACT\*.* E:\ACT
    @echo off
    cls
    exit
     
  2. theefool

    theefool Geekified

    How do you want the date?

    I can think of a few ways to get this accomplished through batch scripting.
    Also, what OS is this?
     
  3. medic42

    medic42 Private E-2

    XP, I want it to change to something like ACT07202005 or ACT072005
     
  4. theefool

    theefool Geekified

    Unless someone beats me to it, I can create a "dos" batch file that can do what you want. I should be able to get it done today, but not for awhile, since I have tons of stuff to do at work.
     
  5. theefool

    theefool Geekified

    Okay, I think I got it:

    @echo off
    Rem :Created by Theefool from MajorGeeks
    Rem :Version 1
    setlocal
    Rem :Create a temp file of the variable date
    echo %date% > test1.txt
    Rem :parse the day word with the actual day/month/year number
    for /F "tokens=2" %%i in (test1.txt) do echo %%i > test2.txt
    Rem :Get rid of the slashes and set variables
    for /F "tokens=1 delims=/" %%i in (test2.txt) do set month=%%i
    for /F "tokens=2 delims=/" %%i in (test2.txt) do set day=%%i
    for /F "tokens=3 delims=/" %%i in (test2.txt) do set year=%%i
    cd /d c:\ACT
    copy c:\ACT\*.* e:\ACT
    e:
    ren ACT ACT%month%%day%%year%
    exit (typically you do not need to add this)

    You can take out the rem(inder) statements if you so wish.

    I hope this helps you out. IF you have problems, feel free to scream at me!

    This took a bit longer, I forgot that within batch files you MUST have %%a variables, not %a variables.

    Also, this only took 3 beers to make.

    Notes:

    The FOR command is very powerful.
    The cd /d command will change your directory and change which drive you are working with!
     
  6. Anon-068c403e2d

    Anon-068c403e2d Anonymized

    to copy subdirectories replace copy with xcopy /s/e
    Also the second time you run the batch you no longer have a folder act in e: as it was renamed,this could be a problem.So using mkdir e:\act would be a good idea.
     
  7. Anon-068c403e2d

    Anon-068c403e2d Anonymized

    this will work as long as you run it only once a day.
     
  8. medic42

    medic42 Private E-2

    This works great, thanks. One minor problem, when I run it, it creates a file (test1.txt) on my desktop and leaves it there. Not sure why, but what do I change so it at least does not leave it there?
     
  9. Anon-068c403e2d

    Anon-068c403e2d Anonymized

    Code:
    @echo off
    mkdir e:\act
    Rem :Created by Theefool from MajorGeeks
    Rem :Version 1
    setlocal
    Rem :Create a temp file of the variable date
    cd %temp%
    echo %date% > test1.txt
    Rem :Parse the day word with the actual day/month/year number
    for /F "tokens=2" %%i in (test1.txt) do echo %%i > test2.txt
    Rem :Get rid of the slashes and set variables
    for /F "tokens=1 delims=/" %%i in (test2.txt) do set month=%%i
    for /F "tokens=2 delims=/" %%i in (test2.txt) do set day=%%i
    for /F "tokens=3 delims=/" %%i in (test2.txt) do set year=%%i
    cd /d c:\ACT
    xcopy /s/e c:\ACT\*.* e:\ACT
    e:
    ren ACT ACT%month%%day%%year%
    exit
     
  10. medic42

    medic42 Private E-2

    That works ... thanks again...
     
  11. theefool

    theefool Geekified

    I never run batch files on the desktop. But, here is a slight modification of that work:

    I noticed some redundancy, and have decided to reduce the size.

    Code:
     @echo off
     Rem :Created by Theefool from MajorGeeks
     Rem :Version 1.1
     setlocal
     Rem :Create a temp file of the variable date
     cd %temp%
     echo %date% > test1.txt
     Rem :Parse the day word with the actual day/month/year number
     for /F "tokens=2" %%i in (test1.txt) do echo %%i > test2.txt
     Rem :Get rid of the slashes and set variables
     for /F "tokens=1 delims=/" %%i in (test2.txt) do set month=%%i
     for /F "tokens=2 delims=/" %%i in (test2.txt) do set day=%%i
     for /F "tokens=3 delims=/" %%i in (test2.txt) do set year=%%i
     md d:\ACT%month%%day%%year%
     cd /d c:\ACT
     xcopy /e *.* e:\ACT%month%%day%%year%
     exit
    /s = copies directories and subdirectories except empty ones
    /e = copies directories ans subdirectories including empty ones
     
  12. theefool

    theefool Geekified

    Yeah, I noticed my mistake earlier this morning, when I woke up.

    Thanks for the modifications, though a simple [erase test1.txt] and erase [test2.txt] will clean this up also.
     
  13. intentsly

    intentsly Private E-2

    I know this is ancient thread, but I'm trying to rename a backup file with the date stamp in XP. This batch file seems that it will do what I want, but when I go to rename my file it says "The syntax of the command is incorrect."
    Any help would be greatly appreciated! Here is my file:

    Rem : The following code was created by theefool at majorgeeks.com forums

    setlocal
    Rem :Create a temp file of the variable date
    cd %temp%
    echo %date% > test1.txt
    Rem :parse the date word with the actual day/month/year number
    for /F "tokens=2" %%i in (test1.txt) do echo %%i > test2.txt
    Rem :Get rid of the slashes and set variables
    for /F "tokens=1 delims=/" %%i in (test2.txt) do set month=%%i
    for /F "tokens=2 delims=/" %%i in (test2.txt) do set day=%%i
    for /F "tokens=3 delims=/" %%i in (test2.txt) do set year=%%i

    Rem : My code added

    rename c:\test.txt %month%-%day%-%year%_test.txt


     
  14. GermanOne

    GermanOne Guest

    It depends on the "international" settings in your registry how the date looks like. For that reason it would be absolutely important to know what ...
    Code:
    echo %date%
    ... would be display in your command prompt.

    How ever. If you're allowed to access your registry (only for reading) then you could try that code:
    Code:
    for /f "tokens=1,2*" %%a in ('reg query "HKCU\Control Panel\International"') do set "%%a=%%c"
    for /f "tokens=1-3 delims=%sDate%" %%a in ("%date:* =%") do (
      if %iDate%==0 (set "mm=%%a" &set "dd=%%b" &set "yy=%%c")
      if %iDate%==1 (set "dd=%%a" &set "mm=%%b" &set "yy=%%c")
      if %iDate%==2 (set "yy=%%a" &set "mm=%%b" &set "dd=%%c")
    )
    
    REM Output for testing.
    echo %mm% %dd% %yy%
    
    ren "c:\test.txt" "%mm%-%dd%-%yy%_test.txt"
    
    Regards
    GermanOne
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds