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

Robopolo

New member
Local time
11:31 AM
Messages
1
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

Computer type
PC/Desktop
Computer Manufacturer/Model Number
my own rig
OS
32 bit, all versions work in an office
CPU
i7 4770k
Motherboard
Gigabyte GA-Z87-D3HP Socket 1150
Memory
16gb Corsair Dominator Vengence 1800 mhz
Graphics Card(s)
Msi nvidia gtx 780
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

Computer Manufacturer/Model Number
HP Media Center
OS
Windows 7 32 bit
CPU
AMD 5200+ dual core
Memory
2 GB
Graphics Card(s)
NVidia GeForce 6150SE 128 MB
Monitor(s) Displays
CRT
Screen Resolution
1280x1024
Hard Drives
500 GB Sata internal :

SIIG USB 3.0 docking stations w/WD Caviar Black 6 Gb/s drives
Keyboard
PS/2
Mouse
PS/2 Wheel Mouse
Other Info
SIIG USB 3.0 PCIexpress card.
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

Computer type
PC/Desktop
OS
Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan

My Computer

Computer Manufacturer/Model Number
HP Media Center
OS
Windows 7 32 bit
CPU
AMD 5200+ dual core
Memory
2 GB
Graphics Card(s)
NVidia GeForce 6150SE 128 MB
Monitor(s) Displays
CRT
Screen Resolution
1280x1024
Hard Drives
500 GB Sata internal :

SIIG USB 3.0 docking stations w/WD Caviar Black 6 Gb/s drives
Keyboard
PS/2
Mouse
PS/2 Wheel Mouse
Other Info
SIIG USB 3.0 PCIexpress card.

My Computer

Computer Manufacturer/Model Number
HP Media Center
OS
Windows 7 32 bit
CPU
AMD 5200+ dual core
Memory
2 GB
Graphics Card(s)
NVidia GeForce 6150SE 128 MB
Monitor(s) Displays
CRT
Screen Resolution
1280x1024
Hard Drives
500 GB Sata internal :

SIIG USB 3.0 docking stations w/WD Caviar Black 6 Gb/s drives
Keyboard
PS/2
Mouse
PS/2 Wheel Mouse
Other Info
SIIG USB 3.0 PCIexpress card.
Back
Top