how to delete a directory in cmd, without deleting its content? - cmd

Im new to cmd, and I was given a task to delete a directory, without deleting its content, in one line.
How can I do that?
I had already tried rmdir and del, but they remove the content of the directory along with it.
I thought about using move first in order to move the content, but I have no idea as for how to do it along with deleting the folder in one line.
Any help would be much appreciated

Like this on all Microsoft OSes since 2000, and still good today:
dir & echo foo
If you want the second command to execute only if the first exited successfully:
dir && echo foo
As answered here.
So you can easily move, then delete the folder all in one line.

Related

Seek a command to move or rename or cut a file on windows

I want to move / cut a file on Windows (PS: the orginal path and destination are on the same drive.)
I know xcopy can copy a file, but I don't know how to cut a file with it. And it came to me the command of rename which is the function name of cutting a file in C/C++.
I tried this, rename command, but it seems it just renames files under a same folder, so it can't do cutting.
And I also don't want to do copy+delete the orignal one work, because a real cut a more effective since it just changes the file's position reference in the directory tree.
Edit: I'm seeking a command working for VisualStudio Post Build Event. It seems Move-Item doesn't work for that.
Thank to #Olaf's hint. I just find the command move, and with the help of if exist ... del, it works well for visual studio build event
IF EXIST "destionation\$(TargetFileName)" (DEL /F "destionation\$(TargetFileName)")
Move $(TargetPath) destionation\$(TargetFileName)

How do I Copy a directory and contents to the same location with a new name

I am going to answer this question myself to get the information out there because I was unable to find the answer on google. I need to run a command that will copy folder foo to the same location as foo and have it be named bar. (see images below)
I tried several variations of the copy command and copy to another location rename then move back, but this was too complex for my taste. Please feel free to expand on my answer if I am missing something.
Thanks
Not saying this is perfect, but this is what worked for me
#first move to the desired working directory
cd C:\temp
#then run the following command using exactly the syntax below
xcopy .\foo .\bar\
This is what worked for me.

Batch file to detect only part of a filename

I need a snippet of a batch file that can detect part of a filename, then rename it.
Note that the numbers after the filename change randomly but "file" is always the same.
Example:
filename: file143424
There will always be only one file that needs to be renamed in the folder. The script will delete older versions of the file. For instance, I put a first file inside the folder and the script in there too. I then run the script, which renames it to just file. Then if I put a new file in, the script, when run again, will recognize it from the "file" prefix and rename it to file after deleting the old one.
Excuse me. Your question is incomplete, I think. You want to rename "file134342" to what?
In your comment you said: rename it to just "file", but this works only if there is just one file with that name. Anyway, here it is:
for %%f in (file*) do ren %%f file
If this is not what you want, then give us more details (you showld always give full details from the very beginning).
You can check for the first four characters that way:
if "%filename:~0,4%"=="file" ...
#Aacini's suggestion should work fine in your case. But you could do very well without a for loop, a single REN (RENAME) command would be enough:
RENAME file* file

changing directory stops %~dp0 from working

I have two batch file on the C:\ drive and am using %~dp0 command to use the path of the first script to make a copy the second batch script:
COPY %~dp0"Hello World.BAT" C:\"Hello World.bak"
Early in the script I am required to change to a sub directory off the root of the C:\ but this stops the above copy command from working the error I get is "the file cannot be found". If I stay in the root of the C:\ the copy command works perfectly. Any ideas why this is happening.
Another way to solve this would be to save %~dp0 in another variable at the beginning of your script.
#echo off
setlocal
set filepath=%~dp0
.
.
some code
.
.
cd away from original path
.
.
COPY "%filepath%Hello World.BAT" "C:\Hello World.bak"
That should work.
I am tempted to think the reason it is not working has to do with your quotes.
You have this:
COPY %~dp0"Hello World.BAT" C:\"Hello World.bak"
replace it with this:
COPY "%~dp0Hello World.BAT" "C:\Hello World.bak"
you need to wrap the entire path in quotes to be sure it will work. If you have:
C:\Program Files\Somefolder\
as your path and use the quotes how you have them it will turn out like this:
"C:\Program Files\Somefolder\""Hello World.bak"
and it won't work.
I haven't exactly worked out in my mind how changing the current directory causes the command to fail when it works before the change. But I notice that the quotes are not placed optimally. Spaces in the path would cause the command to fail, though it seems to me it should fail regardless of your current directory.
I would use:
COPY "%~dp0Hello World.BAT" "C:\Hello World.bak"
Moving the quote to the front of the 1st argument is potentially important. Moving it for the 2nd argument is not important since there are obviously no spaces in the path, but it looks better to me.
edit
After reading your question more carefully, I'm thinking there must be more to the story. If both batch files are in the root of the C drive, then your original posted code should work.
Try editing your script to diagnose what is happening. Put ECHO before the copy command so you can see what the script is attempting to do. (or simply make sure echo is on, but then it may be harder to find the correct line in the output.)
echo COPY %~dp0"Hello World.BAT" C:\"Hello World.bak"
If you still can't figure out what is wrong, post the results so others might help.

How can I move files up one directory and delete old directory without specifically naming files?

I use a shell script that takes a single directory, called NewData, with whatever is inside of it, and creates this:
There is one step I want to add, and I am not sure how to do it. I want to move the contents of NewData and NewDataCopy into their respective parent directories (ProtectedOrig and Data) and delete NewData and NewDataCopy. What command(s) would I add to my script to do this without specifically naming the files to be moved (they will be different every time I run the script)?
If it would help, you can have a look at the script here. I'm grateful for whatever assistance I can get!
You can move everything without naming the files specifically by using a "glob" (aka a "wildcard"). That's a *.
So let's say you are in DataDirectory. You can move everything from Data/NewDataCopy up to Data by doing this: mv Data/NewDataCopy/* Data/. Then delete with a rmdir Data/NewDataCopy.
Starting from DataDirectory then to do everything you'd do this:
mv Data/NewDataCopy/* Data/
rmdir Data/NewDataCopy
mv ProtectedOrig/NewData/* ProtectedOrig/
rmdir ProtectedOrig/NewData

Resources