Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

DOS command help

  • 16-08-2015 7:33am
    #1
    Registered Users, Registered Users 2 Posts: 4,475 ✭✭✭


    Hoping there's some old school DOS gurus out there who can help me with a command or batch file.

    Basically, I have a folder structure like this:
    Folder A
    -- Folder A1
    ---- File A11
    ---- File A12
    -- Folder A2
    ---- File A21
    -- Folder A3
    ---- File A31
    Folder B
    -- Folder B1
    ---- File B11
    ---- File B12
    ---- File B13
    Folder C
    -- Folder C1
    ---- File C11
    -- Folder C2
    ---- File C21
    

    I have a batch file that runs from time to time and deletes the files in these folders based on certain conditions. So after running it today, I might end up with
    Folder A
    -- Folder A1
    -- Folder A2
    ---- File A21
    -- Folder A3
    ---- File A31
    Folder B
    -- Folder B1
    Folder C
    -- Folder C1
    ---- File C11
    -- Folder C2
    

    Now what I'd like to do is iterate through the entire directory structure, and where the second level folders are empty, delete them. So in the example above, delete folders A1, B1 and C2. Note that by deleting B1, folder B is now empty, but I don't want to touch this on this or future runs of the script. This extra condition appears to be the straw that's breaking my back. I can write a script to do the above, but it WILL remove folder B the next time it runs.


Comments

  • Registered Users, Registered Users 2 Posts: 707 ✭✭✭Bayberry


    Without seeing the logic that you use to delete the folders, I can't suggest a test to avoid deleting it if it's a top level folder.

    But if you have a script that can take a single parameter (the root folder to be "cleaned") then you could use the FOR command to apply or call it for each of the root folders in turn.

    FOR /D %f in (*) DO CALL CleanUp.cmd %f

    That would call the CleanUp script for each folder in the current folder. (The /D means that it only returns directory names). If you specify (C:\*), for example, the output will include the C:\. You can test the output with

    FOR /D %f in (*) DO @ECHO %f

    Note that if you write this in a scipt, you'd have to use %%f - the examples above are what you'd use directly on the command line.


Advertisement