Delete specific AppData subfolder file in userprofile (windows 10) - batch-file

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.

Related

Batch file to remove specific subfolders but move everything inside them to the outside first

I am looking for help with a batch script which can help me remove specific subfolders from all directories that are in the same place as the batch file. But instead of deleting everything in the subdirectory, I want to move the contents outside of the subdirectory first.
To elaborate, I receive files which are always nested inside folders with a very specific pattern:
Every request I receive is in a uniquely named folder which follows no specific pattern (the Job Folder).
Inside every Job Folder, there are two things:
A folder denoting the language the file is in (Lang Folder).
An xml file named "Manifest.xml" which contains instructions and metadata.
There is a folder inside every Lang Folder denoting the brand of the file (Brand Folder).
Inside every Brand Folder is a random assortment of subfolders containing JSON files with various degrees of nesting inside different subfolders.
So a typical request would contain the following structure:
Job Folder/Lang Folder/Brand Folder/...
And I want to transform all folders to follow this structure:
Job Folder/Brand Folder/...
You can see an example below of how the folder structure currently looks and how I would like it to look in the end.
Structure of the folders and files before running the batch file:
French_Job1373
French
BrandA
Subfolder
Example.json
Manifest.xml
German_Job1374
German
BrandB
Subfolder1
Subfolder2
Subfolder3
ExampleFile.json
Manifest.xml
Japanese_Job1375
Japanese
BrandC
Subfolder1
Example.json
Manifest.xml
Korean_Job1376
Korean
BrandC
Subfolder1
Subfolder13
ExampleSrc.json
Manifest.xml
Structure of the folders and files as it should be after running the batch file:
French_Job1373
BrandA
Subfolder
Example.json
Manifest.xml
German_Job1374
BrandB
Subfolder1
Subfolder2
Subfolder3
ExampleFile.json
Manifest.xml
Japanese_Job1375
BrandC
Subfolder1
Example.json
Manifest.xml
Korean_Job1376
BrandC
Subfolder1
Subfolder13
ExampleSrc.json
Manifest.xml
I would like to have a batch file which essentially removes the Lang Folder, pulling everything from inside the Lang Folder one level up.
Following should be taken into consideration:
The file Manifest.xml should not be touched. It should remain inside the Job Folder level.
Everything from the Brand Folder level including the Brand Folder itself should be moved one level up (to the same level where the file Manifest.xml is).
The Language Folder (which at this point should be empty) should be deleted.
There is a finite list of possible languages after which the Lang Folder is named. If required to specify all possible languages in the batch file, I would like the option to add new languages to the list in the future.
So far, I have managed to get the batch file below to work as expected, but I need to run as many times as there are Job Folders and I have to place it inside the Language Folder to have it work as expected. If I place it anywhere else, it just deletes the Lang Folder with all of its contents and does not move anything. I am looking to have the batch file check all folders that are next to it and perform the operation as many times as needed.
What I have so far:
#echo off
if -%1==- echo No parameters! You must add %%P parameter! & pause & goto :EOF
cd /d %1
move * ..
for /d %%f in (*) do move %%f ..
cd ..
"%commander_path%\totalcmd.exe" /o /s %1\..
rd %1
FOR /d /r . %%d IN (Russian) DO #IF EXIST "%%d" rd /s /q "%%d"
FOR /d /r . %%d IN (French) DO #IF EXIST "%%d" rd /s /q "%%d"
FOR /d /r . %%d IN (German) DO #IF EXIST "%%d" rd /s /q "%%d"
FOR /d /r . %%d IN (Japanese) DO #IF EXIST "%%d" rd /s /q "%%d"
That is an excellent description of the folder moving task which can be done with a batch file stored in the folder containing all the Job Folders containing the following lines.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "ErrorPause="
for /D %%I in ("%~dp0*_Job*") do for /F "eol=| delims=_" %%J in ("%%~nxI") do if exist "%%I\%%J\" (
for /F "eol=| delims=" %%K in ('dir "%%I\%%J" /A /B 2^>nul') do move "%%I\%%J\%%K" "%%I\" 2>nul || (echo Failed to move: "%%I\%%J\%%K"& set "ErrorPause=1")
rd "%%I\%%J" 2>nul || (echo Failed to delete: "%%I\%%J"& set "ErrorPause=1")
)
if defined ErrorPause pause
endlocal
The first FOR loop searches in folder of the batch file referenced with %~dp0 (path ends always with a backslash) for non-hidden subfolders matching the wildcard pattern *_Job*. So assigned to loop variable I are one after the other the full qualified folder names of the Job Folders French_Job1373, German_Job1374, German_Job1374, Korean_Job1376, ...
The second FOR command splits up the current Job Folder name on underscores with everything up to first underscore assigned to loop variable J. That is the name of the Lang Folder in current Job Folder.
The IF condition checks if the Lang Folder in current Job Folder exists at all as otherwise there is nothing to do for the current Job Folder.
The third FOR loop starts one more command process in background with %ComSpec% /c and the command line within ' appended as additional arguments. So there is executed with Windows installed into C:\Windows and batch file path being C:\Temp, for example, for the first Lang Folder of first Job Folder:
C:\Windows\System32\cmd.exe /c dir "C:\Temp\French_Job1373\French" /A /B 2>nul
The command DIR outputs
all names of the folders and files including hidden ones because of option /A (all attributes)
in bare format because of option /B which means just folder/file name without path
in the specified directory.
FOR ignores folders and files with hidden attribute set which is the reason for using the DIR command line executed by a separate command process in the background.
Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.
FOR with option /F would split up by default all lines captured from handle STDOUT of background command process after started cmd.exe terminated itself into substrings using normal space and horizontal tab as string delimiters, would ignore next the line if the first substring starts with a semicolon being the default end of line character, and would otherwise assign just the first space/tab separated string to specified loop variable K. That behavior would be no problem according to the example for the folders BrandA, BrandB and two times BrandC. But the usage of the option string "eol=| delims=" results in using a vertical bar as end of line character which no folder/file name can contain and the definition of an empty list of string delimiters which disables line splitting behavior. So each folder/file name output by DIR without path is assigned completely to the loop variable K one after the other.
The command MOVE moves the current folder/file in Lang Folder of current Job Folder one level up into the Job Folder. This action is very fast as this is done by just updating the master file table of the file system which is cached by Windows. There is an error message output and the environment variable ErrorPause is defined if an error occurs on moving a folder or file up one level.
The current Lang Folder is removed with command RD after all folders and files in the current Lang Folder are moved up hopefully successfully. The deletion of the folder fails if a folder or file in Lang Folder could not be moved up because of a file in this folder tree is currently opened by an application, or a folder in this folder tree is the current folder of any running process, or a folder/file with same name exists already in Job Folder. An error message is output and the environment variable ErrorPause is defined on deletion of Lang Folder fails because of the folder is not empty.
The command PAUSE halts the batch file execution until a key is pressed if an error occurred. Otherwise the processing of the batch file ends without requiring any further user action.
The batch file can be simply executed once again in case of an error because of a file is opened in an application, or a folder is the current folder of a running process, or the folder/file to move exists already in Job Folder after closing the file in the application or the application itself respectively deletion of the folder/file in Job Folder.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains %~dp0 ... drive and path of argument 0 which is the batch file path.
dir /?
echo /?
endlocal /?
for /?
if /?
move /?
pause /?
rd /?
set /?
setlocal /?
All these commands are internal commands of Windows command processor cmd.exe.
See also: Single line with multiple commands using Windows batch file

Open multiple directories without knowing the names of said directories and delete folders within them using a Windows batch file

Okay, I do not really know how to describe fully what I want and I feel bad for asking something so advanced. I do not know how to do anything other than move folders and delete files and folders using batch.
I am trying to make a batch file to delete certain Steam files and folders which are used to cache data. I could easily make this for myself and call it a day however I would like to share it and have it work for other people also. The problem is one of the cache folders is named with a unique Steam identifier. I will put what I have made so far below.
REM Makes a temporary folder.
mkdir %LocalAppData%\Temp\steam_cache
REM Deleted all none essential files in Steam's configuration folder.
move "%ProgramFiles(x86)%\Steam\config\config.vdf" %LocalAppData%\Temp\steam_cache
move "%ProgramFiles(x86)%\Steam\config\loginusers.vdf" %LocalAppData%\Temp\steam_cache
rmdir "%ProgramFiles(x86)%\Steam\config" /s /q
mkdir "%ProgramFiles(x86)%\Steam\config"
move %LocalAppData%\Temp\steam_cache\config.vdf "%ProgramFiles(x86)%\Steam\config"
move %LocalAppData%\Temp\steam_cache\loginusers.vdf "%ProgramFiles(x86)%\Steam\config"
REM Delete all none essential files in Steam's userdata folder.
I will put what the file structure goes like below.
%ProgramFiles(x86)%\Steam\userdata\256283931
As you can see the number at the end is completely random and I do not know how to open that directory without knowing the number first. There are sometimes multiple of these folders with random names if you login with multiple account and I would like the batch file to go into them one by one and delete certain folder inside of them.
I will put below the folders that I would like to delete that are inside of the random numbered folder below.
%ProgramFiles(x86)%\Steam\userdata\256283931\ugcmsgcache
Sorry if what I am asking it too much if so just ignore this post, thanks.
What you are looking for is the for /D loop, which enumerates directories:
rem // Enumerate all directories under `userdata`:
for /D %%I in ("%ProgramFiles(x86)%\Steam\userdata\*") do (
rem // Check if there is really a sub-directory called `ugcmsgcache`:
if exist "%%~I\ugcmsgcache\" (
rem // Do something with the full path, like echoing, for instance:
echo "%%~I\ugcmsgcache"
)
)
If you want to ensure that the name of the enumerated directory/-ies name consist(s) of decimal figures only, you could use dir and findstr, together with a for /F loop:
rem // Change into parent directory, because `dir /B` returns plain directory names:
cd /D "%ProgramFiles(x86)%\Steam\userdata"
rem // Enumerate all directories under `userdata`, whose names are pure numbers:
for /F "delims=" %%I in ('
dir /B /A:D "*" ^| findstr /I /X "[0123456789]*"
') do (
rem // Do something with the full path, like echoing, for instance:
echo "%%~fI\ugcmsgcache"
)
The cd command could also be replaced by pushd and popd.
The ~f modifier ensures that the full path is derived, related to the current working directory.
Note that the pipe (|) needs to be escaped here, because it is in the set of the for /F loop.

Combining Multiple Folders with prefix and timestamp in folder name into new folder with just prefix

So, this is a particularly puzzling problem for me. I'm open to using whatever I must, but I typically only write simple batch files.
I have a huge research project at the hospital where I work, and the data I'm working with is presented to me with a 4 digit subject identifier followed by a timestamp, as seen in this example:
6443_20170419_141200416
6443_20170419_141200447
6443_20170419_141200500
6476_20170419_141200537
6476_20170419_141201112
etc.
I have literally thousands of these folders, and in each of them is between 1 and 3 files with very long file names - the only commonality is the .DCM extension.
What I'd like to do is have a script that will extract the first 4 characters of the folder name, create a new directory with that 4 character name, and then copy any files located within folders with the matching prefix into the newly created folder.
For example, let's say the folders which all start with 6443 have several .DCM files in them. I want to create a new folder named 6443 (in a different location that the then current directory, to avoid accidental deletion), and then move all of the .DCM files from each directory into the new folder.
I have a .TXT file which contains all of the subject ID #'s that I've been using for various other scripted tasks, using FOR /F to walk this file, if that gives anyone an idea for a solution.
This once is really picklin' my noggin. Help!!!
----- ADDITIONAL INFO -----
I've been making progress, but it's still not right. I'm using the script as shown below, but it's moving ALL of the files in each of the folders to each of the newly created folders, instead of sorting them by 4 digit prefix.
#ECHO on
cls
FOR /f "delims=_" %%a IN ('dir /b /ad "*_*_*"') DO (
if not exist %%a MD .\combined\%%a
FOR /d /r %%d IN ("*") DO (
copy %%d\* .\combined\%%a\*
)
)
Here is the corrected script:
#echo on
cls
for /F "tokens=1* delims=_" %%a in ('dir /B /A:D "????_*"') do (
if not exist ".\combined\%%a\" md ".\combined\%%a"
copy ".\%%a_%%b\*.dcm" ".\combined\%%a"
)
What I changed:
the tokens option has been added to the outer for /F loop in order to be able to rebuild the original source directory name later;
the directory search pattern has been improved;
the if query checked the wrong directory;
the inner for loop has been removed, because it iterated over all source directories again;
the source and destination paths for the copy command have been adapted;
all paths are properly quoted;

Batch file to remove copied shortcuts from public desktop

I currently use this code to copy shortcuts from a folder on a server to C:\Users\Desktop:
if not exist "%1" md "%1"
copy /y "%~dp0PlaceShortcutsHere\*.*" "%1"
This copies any shortcuts I place in the folder to the desktop.
I now need a way to remove these, baring in mind that the shortcuts in the source folder can and will change over time.
Is there a way to compare the shortcuts in the desktop and on the server and only delete the ones that are present in both folders, and only from the desktop of the computer?
These shortcuts are not all of the shortcuts on the desktop of the machines, there are others as well, hence wanting to only delete the ones present in both locations. I will also need this to be adaptive as the shortcuts present on the server will be added to or removed as needed.
This is to be deployed out through SCCM 2007/12 but I want to test it locally first.
And yes, using a GP would be easier but the GP we use has stopped working so I need a backup way of deploying shortcuts.
Took some time but I've found an answer myself. Posting this if it is useful to anyone in the future.
dir "%~dp0PlaceShortcutsHere\*" /a /b /-p /o:gen>>"%~dp0ShortcutList_%date:~-4,4%%date:~-7,2%%date:~-10,2%.txt"
Pushd \\<Server>\<Share>
for /F "delims=" %%G in (ShortcutList_%date:~-4,4%%date:~-7,2%%date:~-10,2%.txt) DO Del "C:\Users\Public\Desktop\%%G"
del "%~dp0ShortcutList_%date:~-4,4%%date:~-7,2%%date:~-10,2%.txt"
Breaking it down:
dir "%~dp0PlaceShortcutsHere\*" /a /b /-p /o:gen>>"%~dp0ShortcutList_%date:~-4,4%%date:~-7,2%%date:~-10,2%.txt"
The above created a text file with the names of the shortcuts in it for referencing by the next line.
Pushd \\<Server>\<Share>
for /F "delims=" %%G in (ShortcutList_%date:~-4,4%%date:~-7,2%%date:~-10,2%.txt) DO Del "C:\Users\Public\Desktop\%%G"
The above maps the server/share that the files are in temporarily, then uses the "For /F" loop to get the file names and then delete them.
del "%~dp0ShortcutList_%date:~-4,4%%date:~-7,2%%date:~-10,2%.txt"
Finally, this line deleted the text file that was created.
This is useful for when the contents of the folder is always changing, it will create an up to date list of the files and then delete the list afterwards, preventing confusion.
%date:~-4,4%%date:~-7,2%%date:~-10,2%
Lastly, this little line will input the current date into the filename of the text documet, so should the final delete line not work you can see the date on it. At a guess I would also think that putting it in paths will make the script only choose the files from the current day, not any others, but I have not tested this.
Please correct me if I'm wrong on anything.

Newbie: Need to find a file, read it, and cd to its directory

EDIT: To clarify what I'm looking to do is move a few files into a data folder of an application. The application is installed into Program Files but it keeps data inside app data. The folder name looks something like this
76053ADAJSQDUC4975
Problem is that it's unique to every instance of the application installed and for every computer that will be using this batch. So I'm in the directory and it has
1AKDHCI4985HF55GHJKB G5586HJFRUK56885KOQQ
The only way to identify the folders is by a .txt file inside each one called
origin.txt
that shows the file path to the applications installation directory (C:/Program Files(x86)/********) on one line.
I figured I can use a for to loop through, find the the file, and read it. What I don't know how to do, is find the right file, and cd to its directory. The second problem is since this batch file will be used by multiple users, not all of their installation paths are the same. So inside .txt it could be C:/, D:/, Program Files or Program Files(x86) so the only thing useful to me is the last several words. How would I go about being selective like that.
I'm currently traveling so can't answer right away but would appreciate if you guys help me out or point me in the right directions. Thanks
First, if the application is installed with an installer which adds something to Windows registry for knowing itself on next run (update/upgrade/uninstall) which version of the application is already installed and into which directory if installed at all, it is better to evaluate this information in registry instead of searching around in file system. See my answer on Locate if the program exists and set a custom variable in batch windows.
Second, with assuming text file origin.txt can be found anywhere within application data directory of current user containing only once (or last) the string :\ as part of the directory with the program files of the application, the following batch code could be used to get the directory path and make it the current directory.
#echo off
set "ApplicationDirectory="
for /F "delims=" %%I in ('dir /A-D /B /S "%APPDATA%\origin.txt" 2^>nul') do (
for /F "delims=" %%D in ('%SystemRoot%\System32\findstr.exe /C:":\\" "%%~I" 2^>nul' ) do (
if exist "%%~D" (
set "ApplicationDirectory=%%~D"
goto ChangeDirectory
)
)
)
echo Could not determine directory of application.
goto :EOF
:ChangeDirectory
cd /D "%ApplicationDirectory%"
echo Directory is: %ApplicationDirectory%
set "ApplicationDirectory="
The first FOR loop uses command DIR to search in all subdirectories of user related application data directory for the file origin.txt. The inner FOR loop is processed if there is at least one such file found.
The inner FOR loop calls FINDSTR to search in found origin.txt for all lines containing the literal string :\ and processes all found lines.
For each line returned by FINDSTR the inner FOR loop checks if there is a directory (or file) using the entire string of the line containing :\.
Both loops are exited immediately on a positive check on existence of directory (or file) with a jump to label ChangeDirectory with the commands to switch the current directory independent on current drive and echoing the found application directory.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
cd /?
dir /?
findstr /?
for /?
goto /?
if /?
set /?
Note: I strongly recommend to run FINDSTR with a better search string or perhaps even a regular expression than just :\ if file origin.txt could contain multiple paths.
I ended up using
set "ApplicationDirectory="
for /r "%appdata%" %%a in (origin.txt*) do find "thewordsiwaslookingfor" <"%%a" >nul && set "ApplicationDirectory=%%~dpa"
Then I used ApplicationDirectory to be able to move some files into that directory.
Which worked perfectly, for what I was trying to do.

Resources