batch&task to create new folder and move all desktop file to it


  1. Posts : 1
    32 bit, all versions work in an office
       #1

    batch&task to create new folder and move all desktop file to it


    hi so work in an recruitment office and im trying to create a batch file that the computers can run on a schedule to create a folder with that days date on it and move all files from the desktop to that folder( also created on the desktop), i want the code to only move files and leave folders and shortcuts unaffected

    this is what i've got so far.

    cd desktop

    for /f "tokens=1-3 delims=/" %%a in ("%date%") do md "%%a_%%b_%%c"
    mkdir c:\users\training\desktop\%date:/=%

    MOVE c:\users\training1\desktop\*.* c:\users\training1\desktop\%date:/=%
    right now it is making the folder but not moving anything i know it has something to do with the \%date:/=%

    il need this batch file to run from a hidden location so that 1. the students dont mess with it and 2. so the batch doesn't move itself from the desktop. also the computers all have different user names so if i could get something that will make "training1" what ever the current logged in user is so i don't have to rewrite the batch for each of the 60 or so computers we have.
      My Computer


  2. Posts : 5,092
    Windows 7 32 bit
       #2

    Especially with files and folders I find AHK easier to figure out than batch. You can download the free AutoHotkey scripting language here:
    AutoHotkey

    Here is a script to get you started. I commented out the actual deletions. You can run it and see the desktop files that would be moved in a msgbox. Each loop it asks if you want to quit to escape the tedium. Once you are sure it will work as expected comment out the lines with msgbox and uncomment the FileMove line

    If anything gets moved into the folder that shouldn't be you can just drag it back during testing. The variable A_Desktop is the user's desktop directory so the script should work without editing as far as that goes.

    Note: free to use at your own risk.

    Code:
    FormatTime,FolderName,,yyyy_MM_dd
    FolderName := A_Desktop "\" FolderName
    FileCreateDir,%FolderName%
    Loop %A_Desktop%\*.*
    {
      If (A_LoopFileName = "desktop.ini")
        continue
      If (SubStr(A_LoopFileName,-3) = ".lnk")
        continue
      ; FileMove,%A_LoopFileLongPath%,%FolderName%
      MsgBox % A_LoopFileName
      MsgBox, 4388, Quit Loop, Quit Loop?
      IfMsgBox,Yes
        ExitApp
    }
      My Computer


  3. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #3

    Anything is more easier to figure out than batch.

    But here's a batch solution that does a similar task.

    Code:
    @echo off
    
    :: Move all files on the users Desktop to Desktop\%date_format%. 
    
    set date_format=dd_MM_yyyy
    set exclude_extensions=".ini",
    
    goto :main
    
    :get_username
    setlocal
    	for /f "tokens=2 delims=\" %%I in (' wmic computersystem get username /value ') do for /f "delims=" %%J in ("%%~I") do (
    		set "user=%%~J"
    	)
    endlocal & set "user=%USER%"
    goto :eof
    
    :get_date date_format_string
    setlocal
    	for /f "usebackq delims=" %%I in (` powershell "(get-date).ToString( '%~1' )" `) do (
    		set datestring=%%I
    	)
    endlocal & set "datestring=%DATESTRING%"
    goto :eof
    
    :SUB filename
    setlocal EnableDelayedExpansion
    	if "%~1"=="%~2" goto :SUB_continue
    	for %%I in ("%~2") do (
    		for %%K in (%EXCLUDE_EXTENSIONS%) do (
    			set ext=%%~K& set ext=.!EXT:.=!
    			if "%%~xI"=="!EXT!" goto :SUB_continue
    		)
    	)
    	move %2 "C:\Users\%USER%\Desktop\%DATESTRING%"
    	:SUB_continue
    endlocal
    goto :eof
    
    :main
    call :get_username
    call :get_date "%DATE_FORMAT:"=%"
    if not exist "C:\Users\%USER%\Desktop\%DATESTRING%" ( md "C:\Users\%USER%\Desktop\%DATESTRING%" ) else ( exit /b 1 )
    for /f "delims=" %%I in (' dir /a:-d /b "C:\Users\%USER%\Desktop" ') do (
    	call :SUB %0 "C:\Users\%USER%\Desktop\%%~I"
    )
      My Computer


  4. Posts : 5,092
    Windows 7 32 bit
       #4

    Pyprohly said:
    Anything is more easier to figure out than batch.
    Even Lisp? :)
      My Computer


  5. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #5

    MilesAhead said:
    Pyprohly said:
    Anything is more easier to figure out than batch.
    Even Lisp? :)
    Even LOLCODE.
      My Computer


  6. Posts : 5,092
    Windows 7 32 bit
       #6

    Pyprohly said:
    MilesAhead said:
    Pyprohly said:
    Anything is more easier to figure out than batch.
    Even Lisp? :)
    Even LOLCODE.
    Never ran into it before. All caps. Way gauche. :)
      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 17:58.
Find Us