Delete folder but exclude specific folder - windows

For student computers I need to cleanup the windows userprofile-folders in (C:\Users*). But I need to keep the following folder (and do it with batch, no powershell-scripts possible, only single commands):
Administrator
All Users
Default
Default.lic
Default User
defaultuser0
Public
40040
40041
I tried this one, but it deleted all folder and didn't exclude anything:
if "%1" == "Administrator" goto End
if "%1" == "All Users" goto End
if "%1" == "Default" goto Ende
if "%1" == "Default.lic" goto End
if "%1" == "Default User" goto End
if "%1" == "defaultuser0" goto End
if "%1" == "Public" goto End
if "%1" == "40040" goto End
if "%1" == "40041" goto End
rmdir /S /Q "C:\Users\%1"
powershell "Remove-Item -Path \"HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\S-1-5-21-21*\" -Recurse"
:End
In my mind is the idea to do with something like an excludelist, but findstr didn't work out. The examples I found where all using only one variable etc. but is this case it should be something in the way:
if C:\Users\* is not C:\Users\*excluded-folder-variable* do rmdir /S /Q "C:\Users\%1"

Try this:
#echo off
setlocal EnableDelayedExpansion
set "exclude=|Administrator|All Users|Default|Default.lic|Default User|defaultuser0|Public|40040|40041|"
cd "C:\Users"
for /D %%f in (*) do (
if "!exclude:|%%~f|=!" equ "%exclude%" rmdir /S /Q "%%~f"
)
You have not shown where your %1 comes from, but you may insert the if "!exclude:... command in your code changing %%~f by %~1 and adjusting subdirectory as needed...

If all the folders to exclude are in the Users folder, it's easiest to use the following code:
for /f "tokens=*" %%d in ('dir /b /ad "%HOMEDRIVE%\Users"') do call :REMOVE "%%d"
goto :EOF
:REMOVE
if /i "%~1" == "Administrator" goto :EOF
if /i "%~1" == "All Users" goto :EOF
if /i "%~1" == "Default" goto :EOF
if /i "%~1" == "Default.lic" goto :EOF
if /i "%~1" == "Default User" goto :EOF
if /i "%~1" == "defaulters0" goto :EOF
if /i "%~1" == "Public" goto :EOF
if /i "%~1" == "40040" goto :EOF
if /i "%~1" == "40041" goto :EOF
rd /s /q "%~1"
You can add or remove any folder you want in the same way.

Related

Get a specific text from a text file using batch

I’ve got a text file named ‘Settings.txt’
Within the file I have “Access: False”
Am I able to grab the text after ‘Access:’ to see what they have inputted?
Here is what I have tried:
for /f "token=2 delims=:" %%L in (Settings.txt) do (if %%L==True goto Start ELSE FALSE goto Fail )
You can incorporate findstr and also you did not use else correctly.
for /f "tokens=2 delims=: " %%i in ('type "settings.txt" ^| findstr /i "Access:"') do (
if /i "%%i" == "True" (
goto :start
) else (
goto :fail
)
)
Obviously there are easier ways, this example does not need else it will fall through to start if the value is True else it will skip that label and goto fail label:
for /f "tokens=2 delims=: " %%i in ('type "settings.txt" ^| findstr /i "Access:"') do if /i not "%%i" == "True" goto :fail
:start
echo do start stuff here:
goto :eof
:fail
echo do fail stuff here
You will need to check for the word Access (unless there is only one line in Settings.txt), to add the SPACE behind the : to the list of delimiters, and you will need to adapt the if syntax and probably make it case-insensitive (/I):
for /F "usebackq tokens=1* delims=: " %%K in ("Settings.txt") do (
if /I "%%K"=="Access" if /I "%%L"=="True" (goto Start) else if /I "%%L"=="False" (goto Fail)
)
Which can be rewritten as this (to make the if blocks more obvious):
for /F "usebackq tokens=1* delims=: " %%K in ("Settings.txt") do (
if /I "%%K"=="Access" (
if /I "%%L"=="True" (
goto Start
) else (
if /I "%%L"=="False" (
goto Fail
)
)
)
)

Moving specific number of files into newly created numbered folders

I currently have around 550 files in a folder with the same format (.csv) and same headers (all started with the letters "YL").
I wonder if there is a way to splits these files (50 files at a time) (order doesn't matter) into numbered folders? (ex. 1, 2, 3, 4, 5) And also create a subsequent folder for the leftover files?
I have found this scripts and tried to modify it for 50 files, but it looks like it only created a the first folder (subdir1)
#echo off
set /a counter=1
set /a filesperfolder=50
cd dir\dir_main
:loopstart
set dirname=subdir%counter%
md %dirname%
echo %dirname%
dir /b | findstr /v /i "subdir*"> %temp%\temp.txt && for /l %%l in (1,1,%filesperfolder%) do #for /f "tokens=1,2* delims=:" %%a in ('findstr /n /r "^" %temp%\temp.txt ^| findstr /r "^%%l:"') do #move %%b %dirname%\%%b >nul
set /a counter=%counter%+1
for /f "tokens=*" %%a in ('type %temp%\temp.txt ^| find /c /v ""') do set _filesmoved=%%a
del %temp%\temp.txt
IF %_filesmoved% LSS 50 goto done
goto loopstart
:done
cls
echo All files were moved!!
pause
exit
I disliked the script you found as it was hard to read and used a temp file to keep track of the list of files. (Also, it evidently doesn't work, so there's that.)
#echo off
SET /a cnt=50
SET /a fnum=0
FOR /F "delims=" %%f IN ('dir /b /a-d *.csv') DO (
CALL :moveFile "%%f"
)
GOTO :end
:moveFile
IF "%cnt%" equ "50" CALL :makeDir
move "%~1" "%fnum%\%~1"
SET /a cnt+=1
GOTO :EOF
:makeDir
SET /a fnum+=1
mkdir %fnum%
SET /a cnt=0
GOTO :EOF
:end
Here is another way to do it. We test if there are still files in the directory, if there is, create a new directory and copy 50 files.
#echo off & setlocal enabledelayedexpansion
set fold_cnt=1
:test
set file_cnt=50
dir /a-d YL*.csv | findstr /IRC:"File(s)"
if %errorlevel% equ 0 (
mkdir !fold_cnt!
) else (
goto :eof
)
for %%i in (YL*.csv) do (
if not !file_cnt! equ 0 (
set /a file_cnt-=1
move /Y "%%i" "!fold_cnt!\%%i"
)
)
set /a fold_cnt+=1
goto test

Use one parameter or the other only & use specified file in batch

I'd like to do two things with my batch script.
The first being use one parameter or the other only. I.e., the following:
C:\> foo.bat file.txt /A
/A commands here.
C:\> foo.bat file.txt /B
/B commands here.
C:\> foo.bat file.txt /A /B
ERROR: Either /A or /B can be specified only.
C:\> foo.bat file.txt /B /A
ERROR: Either /A or /B can be specified only.
And the second being use the specified file to do commands. I.e, the following:
C:\> bar.bat file.txt /A
file.txt has been archived.
I did attempt to write some code to do this, but I'm not getting too far. Here's my work so far:
if /i [%~f1] == [FILE] set usedfile=[FILE]
if /i [%1] == [] goto error
if /i [%2] == [/A] set "A_or_B=A"
if /i [%2] == [/B] set "A_or_B=B"
And then %usedfile% would just be copied to another location with copy.
It seems like the first parameter should be a filename, but I am not quite sure. This should get you closer.
C:>TYPE asdf.bat
#ECHO OFF
SETLOCAL
SET EXITCODE=0
if /i [%~f1] == [FILE] set usedfile=[FILE]
if /i [%1] NEQ [] (SET "usedfile=%~1" & GOTO NextSwitch)
ECHO ERROR: File not specified.
SET EXITCODE=1
GOTO TheEnd
SET "OPT_A="
SET "OPT_B="
:NextSwitch
IF [%2] EQU [] (GOTO SwitchesDone)
if /i [%2] == [/A] (
IF [%OPT_B%] NEQ [true] (SET "OPT_A=true") ELSE (GOTO SwitchError)
)
if /i [%2] == [/B] (
IF [%OPT_A%] NEQ [true] (SET "OPT_B=true") ELSE (GOTO SwitchError)
)
if /i [%2] == [/B] set "OPT_B=true"
SHIFT
GOTO NextSwitch
:SwitchError
ECHO ERROR: Either /A or /B can be specified only.
SET EXITCODE=2
GOTO TheEnd
:SwitchesDone
ECHO NB: usedfile is %usedfile%
ECHO NB: OPT_A is %OPT_A%
ECHO NB: OPT_B is %OPT_B%
:TheEnd
EXIT /B %EXITCODE%
Here are a couple of runs.
21:30:52.27 C:\src\t
C:>asdf.bat adsf /B /A
ERROR: Either /A or /B can be specified only.
21:31:01.01 C:\src\t
C:>asdf.bat adsf /A
NB: usedfile is adsf
NB: OPT_A is true
NB: OPT_B is

Batch parameters with spaces for loop recursive move

I am trying to put a batch together to move all zip files back one directory from the current directory in a recursive move where I specify the search directory from the parent.
In example, the current directory is download and the sub to recursive search is a command line parameter.
I then want to move the *.zip back one directory from download. I don't want to search in any other directory other than the one I specify.
>movezipdir junk 2009
This outputs junk 2009. Then junk was unexpected at this time.
Here's what I have but the for loop doesn't like the variable with spaces...
#echo off
IF %1.==. GOTO No1
set DirName=%*
echo %DirName%
For /d %DirName% in ( *.* ) do (
For /d %%e in (""%DirName%"\*") do (
rem move /Y "%%e\*.zip" "%CD%\.."
echo %DirName%
rem rd /S /Q "%%e"
)
rem rd /S /Q %DirName%
)
GOTO End1
:No1
ECHO No Directory Specified
GOTO End1
:End1
It would be great if someone could point me in the right direction. Thanks. Ken
#echo off
rem :: check if the args is not nul
if [%1] == [] (
goto error
) else (
for /f "usebackq tokens=1*" %%i in ('echo %*') do (
set "dirname=%%~i"
)
)
rem check if the specified args correspond to a directory
if exist "%dirname%\nul" echo %dirname% it's a directory || goto error
set /a c=1
setlocal EnableDelayedExpansion
for /f "tokens=* delims=" %%a in ('dir /b /s /a-d "%dirname%\*.txt"') do (
rem usage: move [{/y | /-y}] [Source] [Target]
rem https://technet.microsoft.com/en-us/library/cc772528.aspx
rem http://ss64.com/nt/move.html
rem %cd% here are redundant because when target are omitted,
rem the target are always the current directory
echo move /-y "%%a" "%cd%"
rem break loop after 5 iterations.
rem You can remove it if the test are Okay
if [!c!] equ [5] goto break_loop
set /a c+=1
)
:break_loop
rem usage: RD [/S] [/Q] [drive:]path
echo rd /s /q %dirname%
exit /b 0
:error
echo Error: No directory specified or invalid arguments
echo Usage: %~0 ^<full path of the directorys^>
echo Example: %~0 c:\user\janne\MyDocuments
exit /b 0

Windows batch script issue

I am having an issue with the following Windows batch script. I have multiple XML files in the %indir% that I want to import one at a time and email the report. Basically the section starting with "if /I %v_continue% == y" is not executing as expected. I did some debugging step by step with echo on, and it goes through the script till the %BLAT% command without executing any of the steps, and then it starts execution with the first "copy %script_path%.....". It executes the import.exe correctly, but then nothing gets assigned to %subj% and subsequently the %BLAT% command fails. Any advice?
Thanks!
#echo off
set environment=%1
set domain=%2
if [%environment%] == [] goto :endofscript
rem - Get the script path
set script_path=%~dp0
rem - Get the script name without the extension
set script_name=%~n0
rem - Get the script name with the extension
rem set script_name=%~nx0
rem - get the script extension
set script_ext=%~x0
rem - Set environment variables
call %script_path%\setenv.cmd
set cnt=0
set filemask=*.xml
for /f %%a in ('dir /b /a-d %indir%\%filemask%') do call :procfile %%a
goto :EOF
:procfile
set impfile=%1
set v_continue=n
set emailyn=y
set trset=%impfile:~0,3%
set /A cnt + = 1
if 1%cnt% lss 100 set cnt=0%cnt%
if %trset% == RCT (
set "subtxt=Receipt Confirmation"
set v_continue=y
)
if %trset% == SHP (
set "subtxt=Shipment Confirmation"
set v_continue=y
setlocal EnableDelayedExpansion
set MOFound=
for /f "tokens=3 delims= " %%f in ('find /i /c "<RefID>MO-ORD</RefID>" %indir%\%impfile%') do (set MOFound=%%f)
if !MOFound! GTR 0 (
copy %indir%\%impfile% %inarchdir%
move %indir%\%impfile% %S_INDIR%\FX%dttmstamp%%cnt%.xml 2>NUL
goto :EOF
)
endlocal
)
if /I %v_continue% == y (
copy %script_path%\%script_name%.dat %infile%
cscript %REPLACEVBS% %infile% "DOMAIN" "%domain%" 1>NUL 2>&1
cd /d %rptdir%
%DLC%\bin\import.exe -b -T d:\tmp -p %pfile%
rem - Check for errors
find /i /c "ERROR:" %rptfile% > NUL
if %ERRORLEVEL% NEQ 0 (
set "subj=SUCCESS: %subtxt% Import Report (%environment%/%domain%)"
set emailyn=y
) else (
set "subj=ERROR: %subtxt% Import Report (%environment%/%domain%)"
set emailyn=y
)
move %rptfile% %logdir%\%script_name%_%datestamp%_%cnt%.prn 2>NUL
move %outfile% %logdir%\%script_name%_%datestamp%_%cnt%.out 2>NUL
if /I %emailyn% == y (
echo Report location: %logdir%\%script_name%_%datestamp%_%cnt%.prn > %msgfile%
%BLAT% %msgfile% -server abc-com.mail.protection.outlook.com -f donotreply#abc.com -s "%subj%" -t %INBEMAIL% -attachi %logdir%\%script_name%_%datestamp%_%cnt%.prn
)
)
del /f /q %infile%
del /f /q %pfile%
del /f /q %msgfile%
:delfiles
rem - Delete log files that are older than 10 days.
PushD "%logdir%" && (
forfiles /M %script_name%_*.prn /D -10 /C "CMD /C del /f /q #PATH" 2>NUL
) & PopD
:endofscript
exit /B

Resources