This question already has answers here:
Delete all files and folders in a directory
(17 answers)
How to delete files/subfolders in a specific directory at the command prompt in Windows
(16 answers)
Closed 19 days ago.
I have a folder on my drive, which I regulary clean.
It has only txt files with sql logs.
How can I write batch script which is going to clean it when I run it ?
What I mean is, I do not want to go to drive, look for the folder, etc. Just have a shortcut on my screen, which is going to turn batch script on.
I tried to write it myslef, and modify it a bit, but its not working.
`#echo off
set "search_folder=C:\path\to\folder"
for /f "delims=" %%f in ('dir /b /s /a-d "%search_folder%\*.txt"') do (
type "%%f" | findstr /v /r "^[ ]*$" > temp.txt
move /y temp.txt "%%f"
)`
it does not delete anything
This is how you schedule a batch https://www.thewindowsclub.com/how-to-schedule-batch-file-run-automatically-windows-7
Code to delete bunch of files https://thegeekpage.com/how-to-delete-all-particular-files-inside-a-folder-in-windows-10/
Related
This question already has answers here:
Get Parent directory of a specific path in batch script
(3 answers)
Closed 2 years ago.
I have the following batch script;
CD /d %searchFolder%
FOR /D /r %%G in ("?earch") DO Echo We found %%G
This returns the folder i was searching for. Although i want to know the parent directory path, not the path of the file-folder itself.
I was thinking about replacing %%G with something that did the trick.
Output with %GG:
C:\Drive\folderToBeSearched\Search
Result i want:
C:\Drive\folderToBeSearched
This did the trick, using %%~dpG also had the backslach so i also had to remove that.
CD /d %searchFolder%
FOR /D /r %%G in ("?earch") DO set dir=%%~dpG
set dir=%dir:~0, -1%
This question already has answers here:
Touch file to update the date/time of file? (Modified Date) (WindowsXP BatchScript)
(3 answers)
Closed 2 years ago.
I want to develop batch having step that it will update timestamps of all files within some directory.
Below command gives list of all files in directories.
Can we redirect output of this command to some other windows equivalent touch command, so as to update the timestamps for all files?
cmd>dir /B
Log_adminUser_1.log
Log_adminUser_2.log
Log_adminUser_3.log
Log_adminUser_4.log
Log_adminUser_5.log
Log_adminUser_6.log
As a trick for touching all files in a directory, you could use this:
#ECHO OFF
FOR /F "delims=" %%G IN ('DIR /B') DO (
COPY /B "%%G"+,, "%%G" > NUL
)
The COPY /B construct is documented in TOUCH on SS64, which also explains some caveats with it.
A very similar question to this has been asked (Put files automatically in folders) however I am struggling to convert the answer in the aforementioned question to suit my needs.
My problem is that I need to move folders into other folders using a section of their name, the question that was answered before was about moving files.
My folders have date and time stamps on them 2016-08-23 15.23.45. I need to move these folders to another folder that has just the date on them 2016-08-23.
As another small request, since I'm not very skilled with windows batch files, could someone please tell me where I will need to put my file paths into the batch file.
I need to move folders into other folders using a section of their name
My folders have date and time stamps on them 2016-08-23 15.23.45. I need to move these folders to another folder that has just the date on them 2016-08-23
Use the following batch file (test.cmd):
#echo off
setlocal enabledelayedexpansion
for /f "tokens=1,2" %%d in ('dir /a:d /b') do (
if not exist %%d md %%d
if [%%e] neq [] move "%%d %%e" %%d >nul 2>&1
)
endlocal
Example usage:
F:\test>dir /a:d /b /s
F:\test\2016-08-23 15.23.45
F:\test\2016-08-23 15.23.46
F:\test\2016-08-23 15.23.47
F:\test\2016-08-23 15.23.48
F:\test>test
F:\test>dir /a:d /b /s
F:\test\2016-08-23
F:\test\2016-08-23\2016-08-23 15.23.45
F:\test\2016-08-23\2016-08-23 15.23.46
F:\test\2016-08-23\2016-08-23 15.23.47
F:\test\2016-08-23\2016-08-23 15.23.48
F:\test>
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
for /f - Loop command against the results of another command.
dir - Display a list of files and subfolders.
if - Conditionally perform a command.
md - Make Directory - Creates a new folder.
move - Move a file from one folder to another.
redirection - Redirection operators.
I've searched high and low for a solution to this problem but have so far drawn a blank. I'm fairly new to batch scripts, so apologies if this turns out to be a stupid question.
I have a list of sql filenames in a .txt file which I would like to merge into a single sql script. The .txt file and the .bat file are in one subdirectory of a root location, and the sql scripts are in a separate subdirectory like so:
root\
batch files\
.bat
.txt
views\
.sql
I can read the file names out the .txt file quite happily and pass them to a subroutine using this:
for /f %%d in (IncludeList.txt) do call :Include_List "%%d"
goto :eof
The part that is causing me problems is the :Include_List subroutine. I need to be able to search the views\ subdirectory for each specified filename and then copy the content of it into a new script.
I tried the following, which I think is the nearest I've got it to working, without it actually working:
:Include_List
for /r %%f in ('DIR /B /S ..\Views\vw*.sql ^| find /i %~1') do type %%f >> _All_views.sql
It seems to be treating each part of this ('DIR /B /S ..\Views\vw*.sql ^| find /i %~1') as a filename and then failing to find any of them. It's also checking inside the batch files\ directory and not the views\ directory.
One additional requirement, is that this is part of a larger batch file, which would need to continue processing after the files had been looped through and written to the newly created _all_views.sql file.
Is what I am trying to achieve even possible with a batch file? Or am I just going to have to manually add each required file into the batch file like this
type ..\Views\[filename].sql >> _All_views.sql
Based on wOxxOm's answer my final solution is as follows:
setlocal EnableDelayedExpansion
for /f %%d in (IncludeList.txt) do (
for /f "delims=" %%f in ('DIR /B /S ..\Views\vw*.sql ^| find /i "%%d"') do type %%f >> _All_views.sql
)
I did away with the subroutine as it was causing issues when placed inside a larger batch file, but a nested for loop works perfectly for what I need.
This question already has answers here:
Batch file to create Folder based on Date and Copy Files from One Location to Another Location?
(1 answer)
Batch process to move file having Date in YYYYMMDD format from one folder to another folder
(2 answers)
Closed 9 years ago.
How to copy a folder which is contain date formate(DDMMYYYY) as folder name and folder name will differ from month to month. How can I create batch file for this.
#echo off &setlocal
set "startfolder=."
set "targetfolder=C:\destination"
cd /d "%startfolder%"
for /f "delims=" %%a ('dir /ad /b ^|grep -E "(([12][0-9]|0[1-9])02|(30|[12][0-9]|0[1-9])(0[469]|11)|(3[01]|[12][0-9]|0[1-9])(0[13578]|1[02]))[0-9]{4}"') do (
md "%targetfolder%\%%~nxa"
copy "%%~a" "%targetfolder%\%%~nxa"
)
findstr doesn't have enough Regex capabilities, you need grep for Windows.