Home > database >  Batch file to rename based on size on disk
Batch file to rename based on size on disk

Time:01-05

I was going to make a batch script to share for ex-Dropbox account holders that will go through and rename a file OR folder if the size on disk is > 1 byte. Using other forums I've managed to come up with the below.

@ECHO OFF
setlocal
set maxbytesize=1
FOR /F %%i IN ('PowerShell -ExecutionPolicy Bypass -File "FileSizeOnDisk.ps1" "E:\Desktop\Folder"') DO set SizeonDisk=%%i AND if %SizeonDisk% LSS %maxbytesize% (ren *.* *.*-DELETED)
EXIT

I just need help to loop the powershell command for every file, folder and files in a folder. I don't know how to properly place 2 commands after a DO and can't figure out the code to get it to work on multiple files.

CodePudding user response:

@ECHO OFF
setlocal ENABLEDELAYEDEXPANSION
for /f "delims=" %%o in ('dir /b /a-d "nameofdirectorytoscan\filemask"') do 
 FOR /F %%i IN ('PowerShell -ExecutionPolicy Bypass -File "FileSizeOnDisk.ps1" "nameofdirectorytoscan\%%o"') DO set "Jumba#%%o=%%i"
set size#
pause

The result is a set of variables named Jumba#nameoffile with values of filesize as returned from Powersmell.

You can then scan the list using

for /f "tokens=2,3delims=#=" %%b in ('set Jumba#') do echo name=%%b, size=%%c
  • Related