For example, cd (echo ..) works in powershell, but how do I get it working in batch (it evaluates the echo first, and so the command is effectively cd ..)? mycommand.exe (ls -fi *.hs -exclude \"#*\" -name -r) is what I'm actually trying to convert (it sends a, completed, filtered file listing to mycommand).
setlocal enabledelayedexpansion
set LIST=
for /r %%F in (*.hs) do (
set "FN=%%F"
if not "!FN:~0,1!"=="#" set LIST=!LIST! "%%F"
)
mycommand.exe !LIST!
would be a rough translation.
add the $ symbol to evaluate the commands in the parens first:
mycommand.exe $(ls -fi *.hs -exclude \"#*\" -name -r)
or
ls -fi *.hs -exclude \"#*\" -name -r | mycommand.exe
If you want to execute the command for each item returned from your ls, you can:
ls -fi *.hs -exclude \"#*\" -name -r | %{mycommand.exe $_ }
Related
I want to append found directories to array.
#!/bin/bash
FILES=()
find . ! \( \( -path './android/out' -o -path './.repo' \) -prune \) -type d -name prebuilts | while read file; do
echo "FILES -->$file"
FILES+=("$file")
done
echo "num of FILES: ${#FILES[#]}"
echo "FILES: ${FILES[#]}"
but result as below:
FILES -->./android/test/vts/prebuilts
FILES -->./android/system/apex/shim/prebuilts
FILES -->./android/system/sepolicy/prebuilts
FILES -->./android/vendor/tvstorm/prebuilts
FILES -->./android/vendor/dmt/prebuilts
FILES -->./android/kernel/prebuilts
FILES -->./android/prebuilts
FILES -->./android/developers/build/prebuilts
FILES -->./android/external/selinux/prebuilts
FILES -->./android/development/vndk/tools/header-checker/tests/integration/version_script_example/prebuilts
num of FILES: 0
FILES:
Why does num of array is 0?
You are populating the array after |, i.e. in a subshell. Changes from the subshell don't propagate to the parent shell.
Use process substitution instead:
while read file; do
echo "FILES -->$file"
FILES+=("$file")
done < <(find . ! \( \( -path './android/out' -o -path './.repo' \) -prune \) -type d -name prebuilts)
If you don't need job control, you can also shopt -s lastpipe to run the last command in a pipeline in the current shell.
#echo off
setlocal
set Folder=C:\Test
set FileMask=*.txt
set OldestFile=
for /f "delims=" %%a in ('dir /b /o:d %Folder%\%FileMask%" 2^>NUL') do (
set OldestFile=%%a
goto Break
)
:Break
if "%OldestFile%"=="" (
echo No files found in '%Folder%' matching '%FileMask%'!
) else (
del "%Folder%\%OldestFile%"
)
Hi, I am trying to delete the oldest file in the Test directory using batch file but I am getting an error after running it with or without an Admin Privilege where 'dir /b /o:d "C\Test" 2>NUL' is not recognized as an internal or external command, operable program or batch file. I am trying to run it on Windows Server 2012 R2 x64 bit. Please help. Thank you.
PowerShell seems a bit more concise. Once you are satisfied that it is deleting the correct file, remove the -WhatIf switch.
C:\src\t>type xxx.bat
#ECHO OFF
SET "Folder=C:\src\t"
SET "FileMask=*.txt"
powershell -noprofile -command "dir %Folder% -filt %FileMask% -file |"^
"sort -p LastWriteTime -d |"^
"select -l 1 |"^
"del -WhatIf"
C:\src\t>xxx.bat
What if: Performing the operation "Remove File" on target "C:\src\t\th.txt".
If you are running PowerShell, the syntax is much cleaner.
PS C:\src\t> type .\xxx.ps1
$Folder = 'C:\src\t'
$FileMask = '*.txt'
Get-ChildItem -Path $Folder -Filter $FileMask -File |
Sort-Object -Property LastWriteTime -Descending |
Select-Object -Last 1 |
Remove-Item -WhatIf
PS C:\src\t> .\xxx.ps1
What if: Performing the operation "Remove File" on target "C:\src\t\th.txt".
I have the following code which creates an array and iterate over a directory and create a subdirectory under each of the element of an array.
#!/bin/bash
cd /var/www
dirs=$(find * -maxdepth 0 -type d)
for dir in "${dirs[#]}"; do
echo $dir
mkdir $dir/backups
done
While it echo's all the directories, it creates a directory only on the last element of the array. What can be the issue?
If you are on bash 4.4 particularly , you can use the readarray feature like bellow. Also using -maxdepth 0 seems not a good option - you probably need to use -maxdepth 1.
#!/bin/bash
cd /var/www
readarray -t -d'' dirs < <$(find . -maxdepth 1 -type d -print0)
for dir in "${dirs[#]}"; do
echo $dir
mkdir $dir/backups
done
But in case you can do the whole thing just with find and mkdir -v (verbose):
$ find . -maxdepth 1 -type d -name 'a*'
./appsfiles
$ find . -maxdepth 1 -type d -name 'a*' -exec mkdir -v {}/backup \;
mkdir: created directory './appsfiles/backup'
Using mkdir -v you get verbose messages from mkdir and you can skip the echo.
If you need the echo anyway, you can do it like:
$ find . -maxdepth 1 -type d -name 'a*' -exec bash -c 'echo $0 && mkdir -v $0/backup' {} \;
./appsfiles
mkdir: created directory './appsfiles/backup'
The issue the array initialization - change it to:
dirs=($(find * -maxdepth 0 -type d))
However, the above statement can be problematic if you have directories that have white spaces in them.
You can use a simple glob instead - it handles white spaces too:
cd /var/www
dirs=(*/)
for dir in "${dirs[#]}"; do
: your code
done
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."
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)