How to delete delete a folder from C:\Users\... via a script - batch-file

I need to delete a folder from C:\Users for any user profile within the Users directory.
I’m needing help putting together a windows batch script to accomplish this.
The folder stores a user config file for an application that needs to be deleted.
Thanks in advance!

Here's an idea:
#Echo Off
(Set UPD=C:\Users)
(Set UDS=SomeDir\TheDir)
For /F "Delims=" %%A In ('Dir/B/AD-R-H "%UPD%"') Do (
If Exist "%UPD%\%%A\%UDS%\" RD/S/Q "%UPD%\%%A\%UDS%")
Just put the path and folder spec within the parentheses on line three

Related

Delete specific AppData subfolder file in userprofile (windows 10)

I need some advice on my plan to create a script file to delete certain files (log files( in the AppData subfolder. the logs is auto generated daily and save in their AppData folder
My plan is:
only to delete a log files inside the log folder that the age more than 10days and remain the new logs inside the folder.
My problem is, there are some folder before the logs folder generated different on each customer. Here the example of 2 sample customer that using the application. The bold subfolder is auto generated folder by system and on each user, it create a random subfolder name except for the last Folder Logs. The logs files that I want to create a script to delete is reside in the Logs folder.
C:\Users\zulhadi\AppData\Local\Apps\2.0\TOHH10RY.RPR\1TK3RWZA.7LL\tmov..tion_c53c7abfec4c3d4d_0001.000c_3bf64a70373000ba\Logs
C:\Users\ainul\AppData\Local\Apps\2.0\ALR6MXVO.1Q5\EXHY50X4.TDT\tmov..tion_c53c7abfec4c3d4d_0001.000c_3bf64a70373000ba\Logs
My question is:
Any idea how can I create a script that delete the logs files in the logs folder if the situation like below as explain. I'm not a very computer background and not very familiar about scripting but do have see some video/tutorial over internet on this basic of scripting of deleting the folder in windows 10.
#ECHO OFF
SETLOCAL
rem The following setting for the source directory is a name
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
FOR /d /r "%sourcedir%" %%b IN (*) DO IF /i "%%~nxb"=="logs" (
rem for each logs directory found, get a list of the .log files
rem sorted in reverse-date order. Skip the first 10 (newest) and delete the rest
FOR /f "skip=10delims=" %%e IN ('dir /b /a-d /o-d "%%b\*.log" 2^>nul') DO ECHO DEL "%%b\%%e"
)
GOTO :EOF
Always verify against a test directory before applying to real data.
The required DEL commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO DEL to DEL to actually delete the files.
Perform a recursive listing of the directorynames starting at the chosen sourcedir (See for /? from the prompt for documentation) and select only those with a "leafname" (final element of the full directoryname) of logs. The full directoryname found is in %%b.
With each such directoryname, perform a directory list of the *.log files using dir producing names-only (/b) and no directorynames (/a-d) in order of reverse-date (/o-d) - see dir/? from the prompt for documentation.
We need the full name reported by dir, so delims is set to nothing, and we need to skip the first 10 lines of the report since we wish to retain the latest 10 files which are the first 10 lines returned as the report is sorted in reverse-date order.
The 2^>nul suppresses the error report generated by dir should there be no matching filenames in the directory. The caret is used as an "escape" character to tell cmd that the > redirector forms part of the dir command, not the for.

How can I create a folder with the same name as the file names in that folder?

What I want to accomplish can be done with for %%i in (*) do md "%%~ni" however this only works if my batch file is in the same folder as the files I want to process. I want to run a batch file from a another folder.
This is what I have tried so far and it's not working. It is still creating the folders in the same folder I run the batch file.
for %%i in ("D:\test1\*") do md "D:\test2\" "%%~ni"
What am I doing wrong? I have not written a script before.
You need to concatenate the path with the name
like this
for %%i in ("D:\test1\*") do md "D:\test2\%%~ni"

create batch file to copy folder contents with dynamic name

I am absolutely brand new to any kind of development but need a batch job to copy a file from one folder to another.
The problem is that the source folder is dynamically named. The folder name will contain the current date and a suffix number (eg. "TestRun_20141106_13") - so I will never be able to determine the 'latest' version of the folder before running the batch / copy job.
Can anyone help please? I know this will be easy for someone but as I said, I am a complete noob!!
Thanks in advance.
Jamie
Yes, I havent been doing .bat for that long either, but i think i can help!
Here is a code for the movement of the file!
Dealing wiwth dynamically named folder...
#echo off
set /p txtfile=Filename without Path assumes c:\:
echo.%txtfile%
copy %txtfile% z:\testing\dealer.txt
echo Come back to this window when Agent is done with process. The copy file will be deleted.
#pause
copy %txtfile% c:\somefolder\namedsuccess\%txtfile%
del z:\testing\dealer.txt
exit
You will have to place your own variables in there my friend!
For moving of the files!
Easy part!
move /-y "Folder Path that files are in*(Any specific keyword?)*" "(Dest. folder)"
#ECHO OFF
FOR /F "TOKENS=*" %%A IN ('DIR "C:\Example" /s /b /a:d') DO SET CurrentDir=%%A
#ECHO.%CurrentDir%
Replace "C:\Example" with the Path your Folders are in,
save it to a File (.bat/.cmd) and execute.
The last step - Echo will return the most bottom foldername.

Windows batch way to replace all files in subdirectories with singular file (copy, rename all files)

I have a good command over cmd commands, but this may require a variable or a loop which is where I fail in batch commands. Please help if you can!
-- Have about 100 subdirectories each has 1-20 HTML files in it. There are about 100 HTML files in the root directory too.
-- Need to replace all HTML files in the above directories with the same HTML source (copy over existing file and keep the name the same). Basically trying to replace all existing files with a redirect script to a new server for direct bookmarked people. We are running a plain webserver without access to server-side redirects so trying to do this just by renaming the files (locked down corp environment).
Seems pretty simple. I can't get it to work with wildcards by copying the same file over to replace. I only get the first file replaced, but the rest of the files will fail. Any one with any advice?
This should do it from the command prompt. Replace % with %% for use in a batch file.
for /r "c:\base\folder" %a in (*.html) do copy /y "d:\redirect.html" "%a"
Without knowing more precisely how you want to update the file content I suggest the following rough approach.
To re-create your example, I had to create some folders. Run this command to do that:
for /l %i in (1,1,20) do mkdir fold%i
I then used this script to create some example files:
#echo off
set number=0
for /d %%i in (c:\Logs\htmltest\*) do call :makefiles %%i
goto :EOF
:makefiles
set /a number+=1
touch %1\file%number%.txt
echo %number% >%1\file%number%.txt
I then used this script to append the text changed to the file. Not sure if that is what you wanted - probably you need something more sophisticated.
#echo off
set number=0
for /d %%i in (c:\Logs\htmltest\*) do #for %%f in ("%%i\*.txt") do call :changetext %%f
goto :EOF
:changetext
echo changing file contents to ^"changed^" for file: %1
echo changed>>%1

Deleting folders present on remote machine using batch file

I want to delete couple of folders using batch file.
But problem is that these folders are present on some other machine.
But I can take the remote access to this machine on my machine.
Please help me with this issue. I don't have any idea about this.
A less comprehensive, but more simple answer...
del /F \\myserver\w_root$\users\selah\selah.txt
del /F \\myserver\w_root$\users\selah\status.txt
As long as you can mount the remote folder with a user who has delete authority, you can simply do something like this:
rem Set up the remote path - assuming it is sharable
set MY_DIR=\\10.1.1.1\some\path
rem Mount the remote path
net use %MY_DIR% %MY_PASSWORD% /USER:%MY_USER%
rem Delete a file
if exist "%MY_DIR%\MyFile" del /F "%MY_DIR%\MyFile" >nul
rem Unmount the remote path
net use %MY_DIR% /delete >nul
If you want to remove folders, you'll just need to make sure the mount point is at least one level above where you'll be deleting.

Resources