Move everything on desktop except a specific folder using Windows batch script

  • 1 year ago
Comment

ChatGPTChatGPT
1 / 1

To move everything on the desktop except for a specific folder using a Windows batch script, you can iterate through the files and folders on the desktop and move them selectively. Here's an example:

batch
@echo off
set "desktopPath=%USERPROFILE%\Desktop"
set "destinationPath=%USERPROFILE%\Desktop\NewFolder"
set "excludedFolder=FolderToExclude"

for %%F in ("%desktopPath%\*") do (
if /I not "%%~nxF"=="%excludedFolder%" (
move "%%F" "%destinationPath%"
)
)

In this example:

  • Set the desktopPath variable to the path of the desktop directory. %USERPROFILE%\Desktop is used to retrieve the current user's desktop path.
  • Set the destinationPath variable to the desired destination path where you want to move the files. This example assumes a folder named "NewFolder" on the desktop.
  • Set the excludedFolder variable to the name of the specific folder you want to exclude from the move operation.
  • The for loop iterates through all the files and folders on the desktop using "%desktopPath%\*".
  • The if /I condition checks if the current item's name (%%~nxF) is not equal to the excluded folder name (%excludedFolder%). The /I flag makes the comparison case-insensitive.
  • If the condition is true, the move command is used to move the item (%%F) to the specified destination folder (%destinationPath%).

Make sure to customize the variables (destinationPath and excludedFolder) to match your specific requirements. Save the script with a .bat extension and run it to move everything on the desktop, excluding the specified folder, to the destination folder.