0

I have many folders and i need to delete from any folder the oldest files in it, the number of files that i need to delete changed any iteration so i tried to do a for loop that in any iteration do date sort, after the oldest file at the top of the file i do "skip=variable" this variable changed any iteration and it's not work.

Is someone have any idea how to solve it in batch file? Thanks!

Reutm
  • 3
  • 3
  • We will need a bit of code to help you. Thanks – acostela Feb 09 '16 at 08:27
  • So you append "skip=variable" to the files you want to skip and it isn't skipping them? Is that the issue? – Neil Feb 09 '16 at 08:40
  • FOR /F "tokens=1,2 delims= " %%i in ( C:\folder\varibales_file.txt ) do (set numOfFiles=%%j & pushd %%i & ( for /f "skip=numOfFiles " %%F in ('dir C:\folder_path_to_delete\%%i /b /o-d') do ( rd /s /q C:\folder_path_to_delete\%%i\%%F))&popd) – Reutm Feb 09 '16 at 08:42
  • the error is: " " was unexpected at this time." – Reutm Feb 09 '16 at 08:50
  • `for /f "skip=numOfFiles " `should be `for /f "skip=!numOfFiles!" ` (and you'd need [delayed expansion](http://stackoverflow.com/a/30284028/2152082) – Stephan Feb 09 '16 at 09:06
  • hi Stephan thanks, i try it too, when i do it there is an error "The system cannot find the file " – Reutm Feb 09 '16 at 09:19

2 Answers2

1

The options of for /F command ("skip... delims... tokens", etc) can not be changed after the for /F command was parsed. You need to change the value of "skip=!numOfFiles!" in a different subroutine, so the for /F command be parsed each time it is executed:

@echo off
setlocal EnableDelayedExpansion

FOR /F "tokens=1,2 delims= " %%i in ( C:\folder\varibales_file.txt ) do (
   set numOfFiles=%%j
   pushd %%i
   call :Sub !numOfFiles!
   popd
)
goto :EOF


:Sub numOfFiles
for /f "skip=%1 delims=" %%F in ('dir "C:\folder_path_to_delete\%%i" /b /o-d') do (
   rd /s /q "C:\folder_path_to_delete\%%i\%%F"
)
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • hi,thanks for your answer, i tried what you said, but it's not work, i got an error: "The system on the command is incorrect" here is the code: @echo off Setlocal EnableDelayedExpansion net use \\haifa-nas\build_results\CleanupLogs\reut_cleanup FOR /F "tokens=1,2 delims= " %%i in ( C:\folder\varibales_file.txt) do ( set numOfBuild=%%j pushd %%i call :Sub !numOfBuild! popd ) goto :EOF :sub numOfBuild for /f "skip=%1 delims=" %%F in ('dir C:\folder_path_to_delete\%%i /b /o-d') do ( rd /s /q C:\folder_path_to_delete\%%i\%%F ) – Reutm Feb 09 '16 at 20:53
  • i've sent it in a few lines but it's appears as one line – Reutm Feb 09 '16 at 20:57
  • I am afraid I don't understand you... You must insert my code in a .BAT file and then execute the file! – Aacini Feb 09 '16 at 21:19
  • ok i've had a problem with a spesific folder, now it's work :) Thanks!! – Reutm Feb 09 '16 at 23:03
0

I did the following changes to your "one-line-code":

  • reformatting into a proper and readable batchfile

  • using numOfOFiles as delayed variable (you did it as a string which couldn't work)

  • enclosed all paths/filenames in quotes to proper work with spaces in direcotries or filenames

  • added delims= for the same reason
  • remove ECHO if the output satisfies you.

(corected code only, didn't check if it does, what you want)

@echo off
setlocal enabledelayedexpansion
FOR /F "tokens=1,2 delims= " %%i in (C:\folder\varibales_file.txt) do (
  set numOfFiles=%%j
  pushd %%i 
  for /f "skip=!numOfFiles! delims=" %%F in ('dir "C:\folder_path_to_delete\%%i" /b /o-d') do (
    ECHO rd /s /q "C:\folder_path_to_delete\%%i\%%F"
  popd
) 

are you sure, you want to use rd (Remove Directory), not del (DELete file)?

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Thanks,i tried it and there is an error "The system cannot find the file ..." although the file exist – Reutm Feb 09 '16 at 09:49