locked
Batch Script Doesnt Delete Folders in a FolderList .txt File :( RRS feed

  • Question

  • My batch file is:

    :Delete
    for /f "delims=" %%f in (7profiledeletelist.txt) do rd /s /q "%%f"
    PAUSE
    exit

     In "7profiledeletelist.txt", there are :

    C:\Users\1* (I tried *.* it didnt work)
    C:\Users\2* (I tried *.* it didnt work)
    C:\Users\3* (I tried *.* it didnt work)
    C:\Users\4* (I tried *.* it didnt work)
    C:\Users\5* (I tried *.* it didnt work)
    C:\Users\6* (I tried *.* it didnt work)
    C:\Users\7* (I tried *.* it didnt work)
    C:\Users\8* (I tried *.* it didnt work)
    C:\Users\9* (I tried *.* it didnt work)
    C:\Users\M* (I tried *.* it didnt work)
    C:\Users\T* (I tried *.* it didnt work)

    But via this txt, bat file can not delete anything. If I remove * and If I write exact name it's working well.

    I want to delete all folders starting with M-T and 1 to 9 in Users. How can I change  "7profiledeletelist.txt" or batch command? Is there a mistaken code in batch script? Thank you in advance :(


    \_(ツ)_/ twitter.com/serdaruzun


    • Edited by serdaruzun Saturday, October 18, 2014 1:51 PM
    Friday, October 17, 2014 10:38 PM

Answers

  • Here the solution, thanks to mc-nd
    for /f "delims=" %%f in (7profiledeletelist.txt) do (for /d %%a in ("%%f") do rd /s /q "%%~fa")


    \_(ツ)_/ twitter.com/serdaruzun

    • Marked as answer by serdaruzun Saturday, October 18, 2014 2:50 PM
    Saturday, October 18, 2014 2:50 PM

All replies

  • for /f %%f in ('dir /B c:\users\j*' ) do @echo %%f


    ¯\_(ツ)_/¯

    Saturday, October 18, 2014 4:52 AM
  • for /f  %%i in ('type 7profiledeletelist.txt') do@echo %%i

    ¯\_(ツ)_/¯


    • Edited by jrv Saturday, October 18, 2014 4:58 AM
    Saturday, October 18, 2014 4:57 AM
  • for /f %%f in ('dir /B c:\users\j*' ) do @echo %%f


    ¯\_(ツ)_/¯

    Where is the 7profiledeletelist.txt ?

    \_(ツ)_/ twitter.com/serdaruzun

    Saturday, October 18, 2014 1:01 PM
  • for /f  %%i in ('type 7profiledeletelist.txt') do@echo %%i

    ¯\_(ツ)_/¯


    That code opens 7profiledeletelist.txt windows, not delete the folders written in 7profiledeletelist.txt

    \_(ツ)_/ twitter.com/serdaruzun

    Saturday, October 18, 2014 1:06 PM
  • Here the solution, thanks to mc-nd
    for /f "delims=" %%f in (7profiledeletelist.txt) do (for /d %%a in ("%%f") do rd /s /q "%%~fa")


    \_(ツ)_/ twitter.com/serdaruzun

    • Marked as answer by serdaruzun Saturday, October 18, 2014 2:50 PM
    Saturday, October 18, 2014 2:50 PM
  • Here the solution, thanks to mc-nd
    for /f "delims=" %%f in (7profiledeletelist.txt) do (for /d %%a in ("%%f") do rd /s /q "%%~fa")


    \_(ツ)_/ twitter.com/serdaruzun

    Unfortunately that is not a good solution.  What I posted was intended to show you how these command actually work.If you want to grab a list then here is how to do enumerate a set:

    This gets the item specified in the list

    for /d  %f in (C:\Users\1* C:\Users\2* C:\Users\3*) do @echo %f

    Use double % inside of a batch file.

    To enumerate a the contents of a file we must do this:

    for /F  %f in ('type c:\scripts\test.txt') do @echo %f

    The single quotes say to execute what is inside.  Use the "tokenizer" if you want to extract a field from a line.

    To list a directory recursively use /R

    Start by reading the documentation:  "FOR/?"


    ¯\_(ツ)_/¯

    Saturday, October 18, 2014 3:56 PM
  • Even better: Do not use a cmd.exe shell script (batch file) at all, and use PowerShell instead.

    -- Bill Stewart [Bill_Stewart]

    Saturday, October 18, 2014 3:59 PM
  • So to get you on a good path try this:

    Assuming a file that looks like this:

    C:\Users\1*
    C:\Users\2*
    C:\Users\3* 
    C:\Users\4*
    C:\Users\5*
    C:\Users\6*
    C:\Users\7*
    C:\Users\8*
    C:\Users\9*
    C:\Users\M*
    C:\Users\T*

    A command like this will enumerate the contents of the file and pass them one at a time to the "do" component of "for"

    for /f %%i in ( 'type  7profiledeletelist.txt' ) do rd /s /q %%i

    It is that simple so don't let other stick you with lines that are intended to be an inside joke.

    If you would learn PowerShell you would not get stuck so often.  In another year there will be no one around to help you with batch.  Wait and see.

    PowerShell:

     Get-Content  7profiledeletelist.txt | %{ Remove-Item $_ -Force }

    You don't have to learn theat old weird batch syntax.


    ¯\_(ツ)_/¯



    • Edited by jrv Saturday, October 18, 2014 4:15 PM
    Saturday, October 18, 2014 4:14 PM
  • jrv, did you try your code? Because it didnt work for folders with *

    But "mc-nd"'s solution works perfect, with * or not....

    By the way I promise to learn PowerShell at the nearest time :)


    \_(ツ)_/ twitter.com/serdaruzun

    Sunday, October 19, 2014 8:47 AM