How to delete a non-empty folder in Batch script? [closed] - batch-file

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 9 months ago.
This post was edited and submitted for review 9 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have tried numerous methods of doing this including Remove-Item, rmdir, del, and rd. I have seen similar questions, but none of the answers have helped me. To be clear, I am not asking for a specific "book, tool, or software library"; I just want a Batchfile command to recursively delete a folder. I'm sorry if I'm doing something wrong; I'm pretty new here.

To delete a non empty folder, you need something called recursion. What we usually read is that we want to "delete a folder recursively", which means to delete a folder and all its contents, including other folder, with their respective contents and folders, and so on.
The command del does it for you. Have you read its help or its documentation? To see that, the command you use in the cmd program is:
del /?
But this does not directly answer your question. What you need to put in your batch file is:
del /s /q [non empty folder name]
"/s" is to delete it recursively
"/q" is to delete it without asking for confirmation of each file or folder being deleted. You may want to remove this item, if you want to choose what will be deleted inside the folder.
You must not write the square brackets I wrote in the command example. Just the folder name, as it is.

For the previous answer, I checked the current Microsoft documentation about commands it has, and it does NOT say clearly that the del command will delete only files.
The command you need to recursively delete a folder, and all files OR folders it contains is:
rmdir [name of the folder] /s /q
Please note the "/s" and "/q" arguments, which have the same meaning as for the del command, but they come AFTER the name of the folder! This is what the command documentation shows, as you may read here.
But there are more possible reasons for the recursive directory deletion failing! If you try to delete a directory that has system files or hidden files, the rmdir command will fail. To solve this problem, you need to do more work. To quote the documentation pointed above:
You can't delete a directory that contains files, including hidden or system files. If you attempt to do so, the following message appears:
The directory is not empty
Use the dir /a command to list all files (including hidden and system files). Then use the attrib command with -h to remove hidden file attributes, -s to remove system file attributes, or -h -s to remove both hidden and system file attributes. After the hidden and file attributes have been removed, you can delete the files.

Related

Lost Source Code of a Batch file that I had eventually converted to an EXE. Anyway to get the source back? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
Improve this question
I was working on a little Batch file script that I had converted into an EXE. I had then lost the batch file but I still have the EXE. Is there a way I can convert it back into a .bat file to get my source code back?
All batch "compilers" are just wrappers for the script that extract them to some directory (usually %TEMP%) and run them. The location in %TEMP% is going to vary by which compiler was used, but here are some of the more common ones and where the script ends up getting extracted:
For all of these, my initial script was called raw_script.bat.
Compiler Name
Location
Hidden Folder?
My Script's Location
Advanced BAT-to-EXE Converter
%TEMP%\<string>\tmp<numbers>.bat
Yes
%TEMP%\wxy\tmp94807.bat
f2ko Bat To Exe
%TEMP%\<string>.tmp\<string>.tmp\<string>.bat
No
%TEMP%\3F11.tmp\3F12.tmp\3F13.bat
Slimm Bat To Exe
%TEMP%\<string>.bat
No
%TEMP%\it.bat
BlackHost Bat to Exe
%TEMP%\<string>.bat
No
%TEMP%\cmd.bat
Gotek BatchCompiler
%TEMP%\<string>\tmp<numbers>.bat
Yes
%TEMP%\ytmp\tmp57317.bat
Bat2Exe.net
%TEMP%\<string>.tmp\<filename>.bat
No
%TEMP%\7zS1034.tmp\raw_script.bat
IExpress
%TEMP%\<string>.tmp\<filename>.bat
No
%TEMP%\IXP000.tmp\raw_script.bat
You may have also used some other compiler that does not extract to %TEMP%, and as long as the script takes input from a set /p command that's later used in an if statement and that variable doesn't use delayed expansion, you can use code injection to extract the text of the script:
"=="" call type %0 >"%USERPROFILE%\Desktop\output.txt" & REM
It's REM and not REM. Note the space at the end.
If that crashes the script, the if statement may not use quotes. If that's the case, use
""=="" call type %0 >"%USERPROFILE%\Desktop\output.txt" & REM
instead. Same code as before, just with an extra " at the beginning.

Batch deleting exact file extensions [duplicate]

This question already has answers here:
Delete file with specific extension in batch file
(3 answers)
Closed 2 years ago.
I have .blk and .blkx files in the same folders, of wich im trying to delete only the .blk file. Ive attempted examples found on this post as in del "\folder\*.blk" /s. I managed to get the chosen files deleted however the .blkx files were deleted too (other files werent so i assume its an issue with both the file having .blk in their extension).
How can i select only the .blk file?
Edit: I dont know why this post was marked a duplicate but the above pinned post does not resolve the problem (wich i already talked about not working)
You must have SFNs (short filenames) enabled on that drive, and the .blkx extension is mapped to .blk in the short 8.3 filename. Because of this, and because wildcards match both long and short names, *.blk returns both .blk and blkx files. To distinguish between them you'll need to run a for loop, check the actual extension, and delete each .blk individually.
for %%b in (*.blk) do #(if /I "%%~xb" == ".blk" echo del "%%~b")
Remove echo from the above to actually delete the .blk files.
To recurse into subdirectories (like del /s) use for /r instead of for.
The command is written as to be used in a batch file (per the question tag). To run it at a command prompt, instead, replace the double percents %%b with single ones %b.

Moving file using cmd? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a file that I have downloaded from Google (it is inside the download folder)
and I want to move it to the autorun folder ( The folder where files run when the computer turns on).
I need to move the file using a cmd command ( the reason why is that it's going to be done using the USB rubber ducky.
I am using windows 10 64 bit if it is any help.
The path where the file is
C:\Users\%USERPROFILE%\Downloads\Test.exe
and the path I want to move it to is
C:\Users\%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
The reason why %USERPROFILE% is that it should work on all computer.
To move a file, you use the move command.
move "%USERPROFILE%\Downloads\Test.exe" "%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
I put quotes around the source and target in case you're one of those people who has spaces in their username for some reason (and the target needs them anyway for the spaces in "Start Menu").
From the output of move /?:
Moves files and renames files and directories.
To move one or more files:
MOVE [/Y | /-Y] [drive:][path]filename1[,...] destination
[drive:][path]filename1 Specifies the location and name of the file
or files you want to move.
destination Specifies the new location of the file. Destination
can consist of a drive letter and colon, a
directory name, or a combination. If you are moving
only one file, you can also include a filename if
you want to rename the file when you move it.
[drive:][path]dirname1 Specifies the directory you want to rename.
dirname2 Specifies the new name of the directory.
/Y Suppresses prompting to confirm you want to
overwrite an existing destination file.
/-Y Causes prompting to confirm you want to overwrite
an existing destination file.
To move file use mv, but not working if you don't do with admin, how you can hack, try superuser tools for windows!

Extract zip contents into directory with same name as zip file, retain directory structure [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
Improve this question
I would like to write a bat script to do the following:
Use 7 Zip to extract files from an existing zip file, into a folder by the same name as the original zip file (bar the .zip extension), and keeping the file & directory structure that was contained in the zip file.
I can extract all the same files into the current directory, by using
"C:\Program Files (x86)\7-Zip\7z.exe" e myZipFile.zip
Reading the help of the 7z-command by just typing "C:\Path To\7-Zip\7z.exe" gets the help with all possible arguments. Here we find the following interesting ones:
e : Extract files from archive (without using directory names)
and
x : eXtract files with full paths
Trial and error shows that the latter is the one fitting your desired behaviour without bigger effort :)
After the comment by #BadmintonCat here is the addition that will create a folder to zip everything into (use as batch script with the file as argument):
#echo off
SET "filename=%~1"
SET dirName=%filename:~0,-4%
7z x -o"%dirName%" "%filename%"
From the help: -o{Directory} : set Output directory. 7z will create the directory if it does not already exist.
Just use the command: 7z x *.zip -o\*

How to write a batch file to permanently append a list of directories without redundancies even though we invoke the batch multiple times?

Assume I want to permanently append the following list of directories (in a trimmed version for the sake of simplicity) to my system (as opposed to my local) PATH environment variable.
c:\company\dept\users
d:\school\teachers
e:\school\students
I have to do this by clicking a batch file (with admin privilege set on). If I am not sure I have already done it, I can do clicking the batch file again but without appending redundancies.
Shortly speaking, how can we write such a batch file? I guess we have to make a test for each item in the list whether or not it has been in the PATH. But how to do this?
What I have done so far is
setx PATH "%PATH%;c:\company\dept\users" /m
setx PATH "%PATH%;d:\school\teachers" /m
setx PATH "%PATH%;e:\school\students" /m
but it is not intelligent to prevent redundancies.
I have no knowledge to implement the redundancy test.
As a general approach, I would suggest looking at for with a delims=; to split %PATH% on semicolons, then compare each part to the path you're considering adding.
If you have specific problems along the way, post them as new questions (or search--they may already be answered) and we'll be happy to answer more.
You can get documentation on the for loop by typing help for at the command line.
EDIT: Here's a better answer, from the SuperUser forum: Append to path without duplicating it

Resources