Rename all files in a folder using batch - batch-file

I would like to create a batch file to rename all the files with extension ".log" in a folder to append with today's date.
For example :
App.log will be appended to App.log06112010
where date is 06112010.
Please suggest

forfiles /m *.log /c "cmd /c ren #file #file06112010"

#!/usr/bin/ksh
export TODAYSDATE=`date "+%m%d%Y"`
umask 000
for filename in $1
do
if [ ! -f $1 ]; then
echo "$filename doesn't exist!"
else
if [ -d $1 ]; then
echo "Skipping directory $filename..."
else
mv $filename $filename$TODAYSDATE
fi
fi
done
Usage: move.sh "*.log"

Related

Search for a specific string in a list of files located in a directory Batch Scripting

how would look for a specific string in a list of files in a given directory using batch? For example the following string RTW4OTO150227074405851I2631911150227CAC.
I Have tried this but it is not working:
for %%f in (payment.*) do findstr /i /m /p /c:"RTW4OTO150227074405851I2631911150227CAC" "%%f"" >> results.txt
There is no need for a FOR loop:
findstr /i /m /p /c:"RTW4OTO150227074405851I2631911150227CAC" payment.* >results.txt
If Powerhsell is an option you can try
Get-ChildItem -Path C:\Myfolder -Filter RTW4OTO150227074405851I2631911150227CAC -Recurse
you can use fnr find string you want support regex plus you can recurse search all sub directory search for specific string then change to any thing you want it
example
.\fnr.exe --cl --dir "c:\" --filemask "*.txt" --find "hi" --replace "bye" --includeSubDirectories
or
dir c:\ /a/b | findstr "RTW4OTO150227074405851I2631911150227CAC" >1.txt

How to find files NOT containing a defined text and output their names into a file?

I am having an application folder with sub-folders and thousands of files in it. I want to write a batch script which lists all the files which DO NOT contain particular text, say SAMPLE_TEXT and redirect output to a file. Please help with the script.
Inspired by http://tobint.com/blog/powershell-selecting-files-that-dont-contain-specified-content/ this powershell worked well for me;
Get-ChildItem -include *.sql -recurse | ForEach-Object { if( !( select-string -pattern "USE " -path $_.FullName) ) { $_.FullName}} > FilesMissingUse.txt
In my case I was searching for database scripts (.sql files) which were missing "USE " string.
This may help you - launch it in the top level folder.
#echo off
(for /r %%a in (*) do find "SAMPLE_TEXT" "%%a" >nul || echo %%a)>file.log
#echo off
findstr /S /M /V "SAMPLE_TEXT" *.* > output.txt
With grep you can use
grep -L Font *.pdf > list_of_files.txt
The -L switch returns only files that do not contain the string "Font."

Converting .sh shellscript file to .bat batch file

I have a .sh script
#!/bin/sh
LOOK_FOR="codehaus/xfire/spring"
for i in `find . -name "*jar"`
do
echo "Looking in $i ..."
jar tvf $i | grep $LOOK_FOR > /dev/null
if [ $? == 0 ]
then
echo "==> Found \"$LOOK_FOR\" in $i"
fi
done
can someone help me in convert this to .bat script which runs on windows.
Regards,
DH
#echo off
setlocal enableextensions
set "LOOK_FOR=codehaus/xfire/spring"
for %%a in ("*.jar") do (
echo Looking in %%a
jar tvf "%%a" | find "%LOOK_FOR%" >nul && echo Found in %%a
)
Assuming, of course, you have jar.exe in the path and the current folder contains the .jar files (the same assumptions in the .sh file)

How do I sort a directory full of pictures into sub-directories based on the image name?

I want to take all of the files in /media/mdrive/dump/:
1COD-234355.jpg
MAK-LXT218.jpg
ZIR-CON145.jpg
And create and sort them into the following directories:
/media/mdrive/dump/1/1COD-234355.jpg
/media/mdrive/dump/M/MAK-LXT218.jpg
/media/mdrive/dump/Z/ZIR-CON145.jpg
How would I do that?
This script takes a directory as the first argument and performs what you need:
#!/bin/bash
DIR="$1"
if [ -z "$DIR" ]; then
echo >&2 "Syntax: $0 <directory>"
exit 1
fi
if [ ! -d "$DIR" ]; then
echo >&2 "\"$DIR\" is not a directory"
exit 1
fi
cd "$DIR"
for file in *.jpg *.JPG; do
first=${file::1}
mkdir -p $first && mv $file $first/;
done
head -c xx will return the first xx characters of its input (here, the filename). mkdir -p will skip directory creation if it already exists.
to make two directories you could try something like
dir "/media/mdrive/dump/1/" :: CD would also work here
mkdir folder 1
mkdir folder 2
from here I think you can continue with your IF statements and so forth.
all you need to do is set the dir commands with the Direct path takes the guess work out.
then to check each just do:
start explorer.exe "the folder's path here"
it should open the folder to view the files

Batch File For loop over a list of file extensions with exclusions

Say i have the following files in a directory
/file.js
/file2.min.js
/file1.js
how can i write a batch for loop such that all ".js" files are picked up but ".min.js" are not and the output of the .js filename can be changed to append .min.js
eg:
for %%A IN (*.js) DO #echo %%A "->" %%~nA ".min.js"
would ideally produce the following, and note the file2.min.js is not displayed to the left.
file.js -> file.min.js
file1.js -> file1.min.js
Thanks for your help.
Just look whether it already contains .min.js:
setlocal enabledelayedexpansion
for %%f in (*.js) do (
set "N=%%f"
if "!N:.min.js=!"=="!N!" echo %%f -^> %%~nf.min.js
)
Not that I disagree with #Joey's solution, but I thought it wouldn't hurt if I posted an alternative:
#ECHO OFF
FOR %%f IN (*.js) DO (
FOR %%g IN ("%%~nf") DO (
IF NOT "%%~xg" == ".min" ECHO "%%f" -^> "%%~g.min.js"
)
)
In the past, when I wanted to do something similar I used the renamex script. Here it is with examples:
Usage: renamex [OPTIONS] EXTENSION1 EXTENSION2
Renames a set of files ending with EXTENSION1 to end with EXTENSION2.
If none of the following options are provided, renaming is done in the current
directory for all files with EXTENSION1.
Where [OPTIONS] include:
-v --verbose print details of files being renamed
-d [directory]
--directory [full path] rename files specified in the directory
-f [filter]
--filter [wildcard]
use wildcard characters (* and ?) for renaming files
-h --help show this help
Examples:
renamex htm html
(rename all .htm files to .html in the current directory)
renamex -v log txt
(show verbose output while renaming all .log files to .txt)
renamex -v -d "D:\images" JPG jpeg
(rename all .JPG files located in D:\images to .jpeg)
renamex -v -d "D:\movies" -f *2011* MPG mpeg
(rename all .MPG files with 2007 in their names, in D:\movies to .mpeg)

Resources