Batch FOR /D /r Output parrent directory [duplicate] - batch-file

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%

Related

Batch file to clean one folder [duplicate]

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/

Batch file renames one file twice [duplicate]

This question already has an answer here:
At which point does `for` or `for /R` enumerate the directory (tree)?
(1 answer)
Closed 5 months ago.
I tried to change names *.txt files, but i have a problem I can't solve.
I would like to give prefixes to *.txt files entered by user input, but the code I have adds, without knowing why, a doubled prefix for one file.
#echo off
SET /p Input=Enter prefix wanted:
Echo You entered: "%Input%"
Pause
for %%a in (*.txt) do ren "%%a" "%Input%%%a"
before using the code:
1.txt
2.txt
3.txt
after using the code with prefix added by user:
test1.txt
test2.txt
testest3.txt
Do you know the solution to my problem?
Use
for /f "delims=" %%a in ('dir /b *.txt') do ...
The problem is that when the file is renamed, the for looks for the next file in the directory - which could be the new name of the file.
The code I've provided builds a file list in memory, then processes the list, so it doesn't see the new name of the file.

what is windows equivalent of Touch command to modify timestamp of files in some directory, how to achieve by windows batch built-in commands [duplicate]

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.

FOR command not executing in script [duplicate]

This question already has answers here:
What is the difference between % and %% in a cmd file?
(2 answers)
Closed 3 years ago.
I receive files via FTP that are placed in a folder called Landing. These files arrive in a folder that is randomly named, for example 000174, and the files inside are named Activity.txt.174 for example.
I have created a small script that allows me to reduce the file name to Activity.txt which works perfectly fine when executed within a command shell but when saved as a batch file it will not execute.
The script line is:
FOR /R %f IN (*.txt.*) DO REN "%f" *.&
I have attempted to add various triggers from other scripts to get this working but when I get this working via CMD it still will not execute from a batch file.
Can anyone help please.
Mike
Change
FOR /R %f IN (*.txt.*) DO REN "%f" *.&
to
FOR /R %%f IN (*.txt.*) DO REN "%%f" *.&
https://ss64.com/nt/for_f.html
Use double percent sign before your variable name.
Batch script
#echo off
FOR /R HERE_YOUR_ROOT_PATH %%f IN (*.txt.*) DO REN "%%f" *.&
#echo on

How to copy a folder which is contain date formate(DDMMYYYY) as folder name [duplicate]

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.

Resources