How to create a current date folder using batch file?


  1. Posts : 5
    Window 7 Pro 32 bit
       #1

    How to create a current date folder using batch file?


    Hi

    I am using following batch file for cleaning the desktop and also for taking backup of desktop:

    @echo off

    set curr_date=%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%

    mkdir "%USERPROFILE%\My Documents\backup\%curr_date%_backup"

    xcopy "%USERPROFILE%\Desktop" "%USERPROFILE%\My Documents\backup\%curr_date%_backup" /D/S/H/V/C/F/K/Y

    for /d /r "%USERPROFILE%\Desktop" %%x in (*) do rd /s /q "%%x"
    for /r "%USERPROFILE%\Desktop" %%x in (*) do del /f "%%x"

    copy "%USERPROFILE%\My Documents\backup\%curr_date%_backup\*.lnk" "%USERPROFILE%\Desktop\"

    del /q "%USERPROFILE%\My Documents\backup\%curr_date%_backup\*.lnk"


    In the first line I am creating a folder with current date, e.g. curr_date_backup. Second, copying the contents of Desktop to the newly created folder. Third, deleting directories from the desktop. Forth, deleting files from the desktop. fifth, Copy back all the shortcuts to the desktop. sixth, deleting all the shortcuts from Current date folder.

    This batch file is working perfectly in Windows XP. It works in win 7 also but the problem is that the name of the current date folder created by first command is "Desktop". when I use command prompt to check the name, the name is correct. I checked the properties of the folder the correct is there also. But in windows the name doesn't match with the expected one.

    I need help. Kindly tell me what's going on? why window 7 is showing directory name as "Desktop"? Is there any other way for creating folder in win7 using batch file.

    thanks
    Attached Thumbnails Attached Thumbnails How to create a current date folder using batch file?-1.jpg   How to create a current date folder using batch file?-2.jpg  
    How to create a current date folder using batch file? Attached Files
    • File Type: bat 1.bat (523 Bytes, 112 views)
      My Computer


  2. Posts : 6,285
    Windows 10 Pro X64
       #2

    Works for me, at least the first two commands work as advertised.

    test.bat
    set curr_date=%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%
    echo %curr_date%"

    mkdir "%USERPROFILE%\My Documents\backup\%curr_date%_backup"
    dir "%USERPROFILE%\My Documents\backup"



    C:\Users\xxxxxxx>set curr_date=2011-11-21

    C:\Users\xxxxxxx>echo 2011-11-21"
    2011-11-21"

    C:\Users\xxxxxxx>mkdir "C:\Users\xxxxxxx\My Documents\backup\2011-11-21_backup"

    C:\Users\xxxxxxx>dir "C:\Users\xxxxxxx\My Documents\backup"
    Volume in drive C is D370_C
    Volume Serial Number is C89E-70E8

    Directory of C:\Users\xxxxxxx\My Documents\backup

    11/21/2011 01:30 PM <DIR> .
    11/21/2011 01:30 PM <DIR> ..
    11/21/2011 01:30 PM <DIR> 2011-11-21_backup
    0 File(s) 0 bytes
    3 Dir(s) 28,082,577,408 bytes free
      My Computer


  3. Posts : 5
    Window 7 Pro 32 bit
    Thread Starter
       #3

    Run "1.bat".
      My Computer


  4. Posts : 6,285
    Windows 10 Pro X64
       #4

    No, I won't run 1.bat, sorry. You said "It works in win 7 also but the problem is that the name of the current date folder created by first command is "Desktop". I took the first two lines and put them in a batch file and they work correctly. They did not create a Desktop folder.

    What happens if you run just the first two lines? Does it work correctly?
      My Computer


  5. Posts : 5
    Window 7 Pro 32 bit
    Thread Starter
       #5

    xcopy behavior is the the problem.
      My Computer


  6. Posts : 6,285
    Windows 10 Pro X64
       #6

    Okay, tried it as far as running xcopy and ity works correctly for me. I end up with a

    C:\Users\xxxxxxxx\My Documents\backup\2011-11-23_backup

    xcopy then copied everything from my Desktop directory into it correctly.

    I'm not seeing the problem you are describing.
      My Computer


  7. Posts : 5
    Window 7 Pro 32 bit
    Thread Starter
       #7

    May be it depends on windows. I am running this batch file on 32 bit OS.
      My Computer


  8. Posts : 1,781
    Windows 7 Professional SP1 32-bit
       #8

    I think the problem might be you have a hidden desktop.ini file in your Desktop folder.

    If I'm right, then this file gets copied along with everything else into your backup folder, and when looking at the backup folder with Explorer, the desktop.ini file (its backup, that is) instructs Explorer to show the backup folder as "Desktop".

    See if a desktop.ini file is present in your backup folder. If yes, delete it. Does this change Explorer's behavior?

    If yes, you've found the problem. From there, all you need to do is modify the xcopy commandline arguments to exclude "desktop.ini" from being copied and you're all set.
      My Computer


  9. Posts : 5
    Window 7 Pro 32 bit
    Thread Starter
       #9

    thanks for the info...
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 11:56.
Find Us