I'm trying to do a replace string operation on all files except those in .git folder and all pycache directories which can appear nested in any nested directory. I'm executing the script in the root directory that contains a .git folder.
for /r /d %%F in (*) do (
echo Checking: %%F
if not "%%~xF"==".sh" if not "%%~xF"==".png" if not "%%~xF"==".bat" if not "%%~dpF"=="__pycache__\" if not "%%~dpF"==".git\" (
echo Found file: %%F
findstr /C:"{{ADDON_NAME_PACKAGE}} {{ADDON_NAME}} {{ADDON_NAME_FULL}}" "%%F" >nul 2>&1
if not errorlevel 1 (
echo %%F
set "file=%%F"
setlocal DisableDelayedExpansion
set "cmd=sed -i -e ""s/%replace_package%/%package_name%/g"" -e ""s/%replace_addon_name%/%addon_short_name%/g"" -e ""s/%replace_addon_name_full%/%addon_full_name%/g"" ""!file!""
endlocal & !cmd!
echo Replaced placeholders in !file!
)
)
)
But I am stuck in the if condition of this loop which still prints out all files in the .git folder even though it is excluded using if not "%%~dpF"==".git\". Why? I can't find any solution to this, not even chat gpt can tell me. How is that even possible that the AI doesn't know this? Also if not "%%~dpF"=="%CD%\.git\" does not work.
for /r /d %%F in (*) do (
echo Checking: %%F
if not "%%~xF"==".sh" if not "%%~xF"==".png" if not "%%~xF"==".bat" if not "%%~dpF"=="__pycache__\" if not "%%~dpF"==".git\" (
echo Found file: %%F
which still prints out the files in the .git folder and __pycache__ folders. This is so easy to achieve using Unix/Linux
replace_package="{{ADDON_NAME_PACKAGE}}"
replace_addon_name="{{ADDON_NAME}}"
replace_addon_name_full="{{ADDON_NAME_FULL}}"
perform_replacements() {
local file="$1"
local package_name="$2"
local addon_short_name="$3"
local addon_full_name="$4"
sed -i "s/${replace_package}/${package_name}/g; s/${replace_addon_name}/${addon_short_name}/g; s/${replace_addon_name_full}/${addon_full_name}/g" "$file"
}
find . ( -name .git -o -name pycache ) -prune -o ( -type f -not -name ".sh" -not -name ".png" ) -print0 | while IFS= read -r -d '' file; do
perform_replacements "$file" "$package_name" "$addon_short_name" "$addon_full_name"
echo "Replaced placeholders in $file"
done
So I don't understand why is it so complicated on windows .bat files to do the same? What gives?